FileNotFound exception when trying to write to a file

Posted by Chris Knight on Stack Overflow See other posts from Stack Overflow or by Chris Knight
Published on 2010-03-29T21:30:59Z Indexed on 2010/03/29 21:33 UTC
Read the original article Hit count: 396

Filed under:
|
|
|

OK, I'm feeling like this should be easy but am obviously missing something fundamental to file writing in Java. I have this:

File someFile = new File("someDirA/someDirB/someDirC/filename.txt");

and I just want to write to the file. However, while someDirA exists, someDirB (and therefore someDirC and filename.txt) do not exist. Doing this:

BufferedWriter writer = new BufferedWriter(new FileWriter(someFile));

throws a FileNotFoundException. Well, er, no kidding. I'm trying to create it after all. Do I need to break up the file path into components, create the directories and then create the file before instantiating the FileWriter object?

© Stack Overflow or respective owner

Related posts about java

Related posts about file