0

Quite simply:

File account = new File("./data/account");
account.createNewFile();

Gives me:

java.io.IOException: No such file or directory
  at java.io.UnixFileSystem.createFileExclusively(Native Method)
  at java.io.File.createNewFile(File.java:900)
  ...

Why Does file.createNewFile() give me a IOException with the message No such file or directory? I'm telling it to create the file.

Running this code outside of NetBeans seems to work with no problem, can NetBeans not handle relative file links?

Thanks in advance for any help!

3
  • 1
    Does the directory ./data exist? Commented May 2, 2012 at 2:07
  • . may refer to a different directory when running in Netbeans than the command line. Commented May 2, 2012 at 2:10
  • @Jeffrey . I feel a bit like an idiot now, no ./data does not exist. It did before I migrated the project into netbeans, that could explain it... Thanks! Commented May 2, 2012 at 2:12

2 Answers 2

2

If ./data does not exist, that call will fail.

File f = new File("./data/account");
if(!f.getParentFile().exists()) { // if the directories don't exist
    if(!f.getParentFile().mkdirs()) { // if making the directories fails
        // directories weren't created, throw exception or something
    }
}
f.createNewFile();
Sign up to request clarification or add additional context in comments.

1 Comment

Cool. Thanks. Appreciate it. Will this have any undesirable affects if the current path does already exist though?
1

Netbeans is running the java program from the dist folder. You would need to create the data folder in there. However, I believe in some cases, Netbeans will clean out the entire folder and therefore delete it. I would use an absolute path.

2 Comments

Let's say to avoid this issue I want to create account if it doesn't already exist, and also create ./data too.
That would be the safest route.

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.