0

Assume final String fname = "/dir1/dir2/fname.ext". I do not wish to parse the string recursively in order to create the directories if they do not exist, and only then write to a file. I wish to use the given string, fname, for creating the directories and file if each of which does not exist.

2
  • 1
    Perfect, now we know what you like to do... what have you tried so far? Commented Jul 8, 2012 at 7:44
  • Duplicate:stackoverflow.com/questions/6142901/… Commented Jul 8, 2012 at 7:46

2 Answers 2

2

This is the code you are looking for:

File myFile = new File("/dir1/dir2/fname.ext");
myFile.getParentFile().mkdirs();
// do your writing being sure the parent directories exist.
Sign up to request clarification or add additional context in comments.

2 Comments

Please correct to myFile.getParentFile().mkdirs() so I could mark your post as an answer; and... thank you.
@MrRoth I think it was long corrected before you post your comment.
1

You can use mkdirs to create the path.

File f = new File("/dir1/dir2/fname.ext");
f.getParentFile().mkdirs();

And then work on the file itself.

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.