0

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.

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.

Marsroverr
  • 531
  • 1
  • 5
  • 19
  • I'm getting filePath from a JFileChooser instance. I'm confident the string format is not the issue as the file chooser is able to select and save to a file on the desktop without error. – Marsroverr Aug 26 '20 at 21:02
  • How are you getting the selected file from the JFileChooser? Its `chooser.getSelectedFile()` will return a `File` object so your `new File(filePath)` wouldn't be needed, which is throwing me off. (but really this should make no difference since it's working for you saving to the desktop) – Stephen P Aug 26 '20 at 23:03
  • If the user doesn't specify an extension the filePath doesn't include one so I have to add it on manually. – Marsroverr Aug 27 '20 at 00:39

3 Answers3

1

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...

  • Seems to work, however FileDialog is missing some features of JFileChooser like support for filename filters (the current implementation apparently doesn't work on Windows). – Marsroverr Nov 02 '20 at 20:18
-2

Heres what I would do to try to debug this:

  1. Should you be trying to save to 'My Documents' as opposed to 'Documents'?
  2. Try to save the file to C:\Users\username\test.txt (I suspect that will work)
  3. Try to save the file to C:\Users\username\My Documents\test.txt
  4. If you really do want to save it to 'Documents' make sure the 'Documents' director exists.
  5. If thats the case, open the properties on Documents under the security tab select Everyone under 'Group our user names' and 'Full control' under permissions. that will open it wide up and should allow file creation. you might want to take note of what the settings where so you can put them back as setting a directory to wide open permissions can be 'problematic'. try running the program again.
Marsroverr
  • 531
  • 1
  • 5
  • 19
Dave
  • 17
  • 2
  • The Documents folder exists, My Documents does not. I can save to the user folder oddly enough. All users have full permissions on the Documents folder – Marsroverr Aug 27 '20 at 00:37
-2
  • 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

Kris
  • 13
  • 4