7

This will be a really dumb question, but i can't seem to create a new file in java to save my life.

It always throws

java.io.FileNotFoundException: Users/username/Documents/testProject/test.txt (No such file or directory)

I have tried like this:

File newFile = new File("Users/username/Documents/testProject/test.txt");

and tried this:

File newFile = new File("/Users/username/Documents/testProject/test.txt");

What am i doing wrong?

Edit: apparently the issue wasn't there. I was trying to read from an empty file later on in the code, sorry folks.

8
  • 5
    You are assuming that calling new File() actually creates a file. It doesn't. Commented Jun 13, 2017 at 13:46
  • 3
    Have you tried newFile.createNewFile() before trying to write anything? And make sure to write down correct path Commented Jun 13, 2017 at 13:47
  • What happens when you run a ls /Users/username/Documents/testProject/test.txt from the terminal ? Also its not clear what you are trying to do after you create the File object. Commented Jun 13, 2017 at 13:47
  • 2
    File constructor does not throw this exception. What are you really doing? Commented Jun 13, 2017 at 13:47
  • 1
    new File("...") only creates a reference to a file path; in order to create a file, you would either write to it, or use .createNewFile() Commented Jun 13, 2017 at 13:48

2 Answers 2

18

Contrary to what its name might trick you to believe, new File("...") does not create a new file.

It creates a new object (in memory) containing a pathname.

You can then perform operations like exists(), canRead() and isDirectory() on it, and you can invoke createNewFile() to create an actual file on disk using this pathname.

exists() should return false before createNewFile() is invoked, and it should return true after createNewFile() is invoked. Same for canRead().

Welcome to the wonderful world of programming, where naming things correctly is of paramount importance, and yet hardly ever does anyone do it right. The File class is one of the worst-named classes in Java. It should have been called FilePath or something like that.

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

2 Comments

I did that, and the ide says that createNewFile() is ignored. canRead() and the like return false.
The IDE cannot possibly be saying that createNewFile() is ignored. It may be saying that the result of createNewFile() is ignored. Which means that you should not ignore the result.
1

Additionally to Mike's answer you will probably need to put double // rather than just a single / as it is used as an escaping sequence. I am not sure if this applies in every situation but in case you still get any errors try this.

2 Comments

This fixed my problem and I was getting the exact same errors as described above. Thank you
File.separator should be used instead of \ or / anyway in order to be able to work on any platform.

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.