英文:
Cannot write file to Documents folder
问题
我正在尝试在Java中写入文件,如果文件不存在则创建一个新文件。(使用JDK 14)。然而,当我运行以下代码时,如果文件不存在,在if语句条件处会抛出IOException,内容为“系统找不到指定的文件”;如果文件存在,则会抛出“拒绝访问”。
```Java
File file = new File(filePath);
System.out.println(filePath); // C:\Users\username\Documents\test.txt
if (file.createNewFile()) {
System.out.println("文件创建成功");
} else {
System.out.println("文件已存在");
}
当我尝试将文件保存到桌面文件夹时,代码可以正常工作并成功保存文件,但出于某种原因,它无法访问“文档”文件夹。
作为IntelliJ的用户,我对计算机上的所有文件具有完全访问权限,以管理员身份运行IDE也无法解决问题。然而,我可以保存到用户文件夹和桌面。只有“文档”文件夹或其子目录我无法保存。
网站上有一些类似的问题,比如这个问题,然而原因与我的情况不同,这是一个权限问题,而不是缺少目录的问题。
<details>
<summary>英文:</summary>
I'm trying to write to a file in Java, or create a new file if the file doesn't exist. (Using JDK 14). However, when I run the following code I get an IOException at the if statement condition that reads `The system could not find the file specified` if the file doesn't exist, and `Access is denied` if the file does.
```Java
File file = new File(filePath);
System.out.println(filePath); // C:\Users\username\Documents\test.txt
if (file.createNewFile()) {
System.out.println("File successfully created");
} else {
System.out.println("File already exists");
}
The code works when I attempt to save it to the desktop folder and saves the file successfully, but for whatever reason isn't allowed to touch Documents.
The user I'm running IntelliJ as has full access to all files on the computer, and running the IDE as administrator did not fix the problem. However, I can save to the user folder and the desktop. It is only Documents or child directories of it that I can't save to.
There are a few similar questions on the site such as this one, however the cause is not the same as in my case this is a permissions issue, and not an issue of a missing directory.
答案1
得分: 1
我刚遇到了同样的问题。似乎在某些 Windows 10 安装中,JFileChooser() 不会通知操作系统用户已选择了文件夹,因此沙箱、恶意软件控制和访问控制会阻止访问以创建文件,即使用户具有完全访问权限(权限检查是正常的,但文件写入失败并显示 IOException 13 或访问被拒绝)。然而,FileDialog() 在 JFileChooser 失败的情况下却可以正常工作...
英文:
I've just hit this same issue. It seems that JFileChooser() on some Windows 10 installations doesn't tell the os that the user has selected a folder and as such Sandboxing, Malware Control, Access control blocks access to create a file even though the user had full access (permission checks are OK but file write fails with IOException 13 or Access Denied). However FileDialog() DOES work where JFileChooser fails...
答案2
得分: -2
- 1
如果您想将数据写入文件,只需使用任何Writer
。例如:
FileWriter writer = new FileWriter(yourFile);
您可以使用流过滤以获得更快的操作。展示更多您的代码。您只是展示了现有文件的条件。
- 2
如果您想在“文档”文件夹中创建文件,获取路径,然后执行以下操作:
File file = new File(documentPath);
while (!file.exists())
file.createNewFile();
//文件存在的条件...
如果我没有帮助到您,请在下面评论,只是我无法理解您的问题:)。祝好运
英文:
- 1
if you want write data to file just user any Writer
For example:
FileWriter writer = new FileWriter(yourFile);
You can use stream filtering for faster action.
Show more your code. You've just showed condition for existing file.
- 2
if you want create file in Documents folder, get a path, then make a:
File file = new File(documentPath);
while(!file.exists())
file.createNewFile();
//condition for file existing...`
If I don't help, comment below, just I can't understand your question :). Good Luck
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论