Question

I create new file in new directory:

File logFile = new File("C:/test/test/test.txt");
logFile.getParentFile().mkdirs();
logFile.createNewFile();

And if I agin try create new file (whithout filename extension, only "test" file):

File logFile = new File("C:/test/test");
logFile.getParentFile().mkdirs();
logFile.createNewFile();

I get:

FileNotFoundException (Access is denied)

Was it helpful?

Solution

In modern versions of Windows, regular users don't have access to write to c:/ without administrative privileges. Your code looks OK (in terms of the use of mkdirs()) but I think you're running into this problem.

Try creating a directory in c:\users\YourUserName instead -- this is an issue with your java program lacking administrative privileges to write to c:\

OTHER TIPS

Simple: C:/test/test is an existing directory, and createNewFile will fail on that. The exception has just a very misleading name, FileNotFoundException.

(Furthermore createNewFile() in general is not needed, but I take it, after that you only open the file for appending.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top