281

I want to write a new file with the FileWriter. I use it like this:

FileWriter newJsp = new FileWriter("C:\\user\Desktop\dir1\dir2\filename.txt");

Now dir1 and dir2 currently don't exist. I want Java to create them automatically if they aren't already there. Actually Java should set up the whole file path if not already existing.

How can I achieve this?

5 Answers5

473

Something like:

File file = new File("C:\\user\\Desktop\\dir1\\dir2\\filename.txt");
file.getParentFile().mkdirs();
FileWriter writer = new FileWriter(file);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 14
    Why getParentFile and not just mkdirs? – sauperl Mar 11 '16 at 15:54
  • Will it override the previous folder, if I am reissuing the same code with different sibling file? – surajs1n Nov 17 '19 at 14:32
  • 2
    @surajs1n: If the directory already exists, `mkdirs` will do nothing. – Jon Skeet Nov 17 '19 at 18:14
  • 7
    @sauperl: If the file doesn't exist yet, _mkdirs()_ will assume everything specified is a directory and creates it as such (just tested it). By using _getParentFile()_, you leave the creation of the file itself to the _FileWriter_. – h4nek Dec 07 '19 at 10:36
172

Since Java 1.7 you can use Files.createFile:

Path pathToFile = Paths.get("/home/joe/foo/bar/myFile.txt");
Files.createDirectories(pathToFile.getParent());
Files.createFile(pathToFile);
cdmihai
  • 3,008
  • 2
  • 20
  • 18
  • 5
    Important to keep in mind that relative paths might cause null pointer exception. `Path pathToFile = Paths.get("myFile.txt"); Files.createDirectories(pathToFile.getParent());` – Mag May 23 '17 at 09:00
  • if(!Files.exists(pathToFile.getParent())) Files.createDirectory(pathToFile.getParent()); //Test if the dir already exists to avoid error – Andre Nel Nov 14 '18 at 12:57
  • @AndreNel JavaDoc of `createDirectories` states: Unlike the createDirectory method, an exception is not thrown if the directory could not be created because it already exists. – Jens Piegsa Jul 04 '21 at 14:01
32

Use File.mkdirs():

File dir = new File("C:\\user\\Desktop\\dir1\\dir2");
dir.mkdirs();
File file = new File(dir, "filename.txt");
FileWriter newJsp = new FileWriter(file);
Armand
  • 23,463
  • 20
  • 90
  • 119
18

Use File.mkdirs().

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
4

Use FileUtils to handle all these headaches.

Edit: For example, use below code to write to a file, this method will 'checking and creating the parent directory if it does not exist'.

openOutputStream(File file [, boolean append]) 
Belle
  • 71
  • 1
  • 2
  • 13
kakacii
  • 728
  • 3
  • 10
  • 24
  • 2
    Please, could you be more specific? – Jean Apr 11 '13 at 09:47
  • Hi Jean, Edited. There is a whole bunch of other useful methods under FileUtils. Apache Commons IO classes such as OIUtils and FileUtils makes java developers' life easier. – kakacii Apr 12 '13 at 02:14
  • 1
    I agree FileUtils is a good way to go, but I think an easier way to this is using writeStringToFile, not openOutputStream. E.g. File file = new File("C:/user/Desktop/dir1/dir2/filename.txt"); FileUtils.writeStringToFile(file,"foo bar baz",true); – paul Aug 07 '13 at 22:24
  • Thanks for that . Made my code much cleaner now. Link to recent javadoc : https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html – Nikhil Sahu Jul 09 '16 at 17:39