0

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)

5
  • Nothing wrong in your code to give fileNotFoundException. Did you run the program in different users ? Commented Apr 17, 2014 at 13:36
  • I think, what problem in create object. I try create object with similar name of directory. I think problem in this, but i don't know how it's fix. Commented Apr 17, 2014 at 13:44
  • mkdirs() - First it will check the directory existence. if exists it returns false. So it is not problem if your directory already exists Commented Apr 17, 2014 at 13:47
  • Then i run "C:/test/test.txt" all work. Commented Apr 17, 2014 at 13:51
  • Are you doing something else . you might not have (Running user) permission to create file to get during FileNotFoundException ( Access Denied) . else are you sure this exception throws in this line ? Commented Apr 17, 2014 at 14:02

2 Answers 2

3

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:\

Sign up to request clarification or add additional context in comments.

5 Comments

Problem is not this. I try create new file in D:/ and result is similar.
What user are you? Are you sure you have write access to those folders? Have you tried writing to your own user directory as I suggested? The error you're getting is very straightforward: access denied means you lack the permissions to do what you're trying to do.
When i run first code, directory and file is created. This is mean, what i have access to create files, no?
Did you used the same user to execute next time ?
Yes. I think problem is not user.
1

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

4 Comments

"C:/test/test/ " is an existing directory . not C:/test/test/test . As per the code he given
@Joop Eggen. Even when you try to create file C:/test/test - it would return false and will not through FileNotFoundException ( atleast in java 7)
Yes. mkdirs return false. But problem in logFile.createNewFile(). If first create "C:\test\test\test.txt", then try create "C:\test\test" folder, when i see Exception: The system cannot find the path specified
even the createNewFile would return false, if you are giving folder path

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.