0

I'm trying to create an empty .properties file on my filesystem using java.io.File.

My code is:

File newFile = new File(new File(".").getAbsolutePath() + "folder\\" + newFileName.getText() + ".properties");

if (newFile.createNewFile()){
    //do sth...
}

It says that it's impossible to find the specified path. Printing the Files's constructor's argument it shows correctly the absolute path.

What's wrong?

5
  • 1
    1- You can just use new File("folder", newFileName.getText() + ".properties") and it will store the file in the folder directory relative to your current location; 2. Make sure the path exists, as it will not be created for you by createNewFile (you can use newFile.getParentFile().mkdirs()) Commented Jul 6, 2015 at 7:02
  • Many thanks, it works! In any case I would adopt your cleaner solution -but- why shouldn't works mine? Commented Jul 6, 2015 at 7:24
  • I don't really know, so much context is missing, but to start with new File(".").getAbsolutePath() + "folder\\" doesn't have a separator between the "." and "folder", so you end up with ".folder" instead (filling in for the absolute path instead of ".", but you get the idea) Commented Jul 6, 2015 at 7:27
  • Did you check write permissions for the target directory? Commented Jul 6, 2015 at 7:29
  • That was... I couldn't expected something so trivial. Thanks twice MadProgrammer! Commented Jul 6, 2015 at 7:31

3 Answers 3

2
  1. You can use new File("folder", newFileName.getText() + ".properties") which will create a file reference to the specified file in the folder directory relative to the current working directory
  2. You should make sure that the directory exists before calling createNewFile, as it won't do this for you

For example...

File newFile = new File("folder", newFileName.getText() + ".properties");
File parentFile = newFile.getParentFile();
if (parentFile.exists() || parentFile.mkdirs()) {
    if (!newFile.exists()) {
        if (newFile.createNewFile()){
            //do sth...
        } else {
            throw new IOException("Could not create " + newFile + ", you may not have write permissions or the file is opened by another process");
        }
    }
} else {
    throw new IOException("Could not create directory " + parentFile + ", you may not have write permissions");
}
Sign up to request clarification or add additional context in comments.

Comments

1

I think the "." operator might be causing the error not sure what you are trying to do there, may have misunderstood your intentions but try this instead:

File newFile = new File(new File("folder\\").getAbsolutePath() + ".properties"); 

1 Comment

No, it doesn't, you can use new File(".") to create a file reference for the current working directory, just like new File("folder"); will create a File reference to the file/directory called folder in the current working directory
0

Trivially I missed that new File(".").getAbsolutePath() returns the project's absolute path with the . at the end so my folder whould be called as .folder. Next time I'll check twice.

Comments

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.