Directory does not exist with FileWriter

I use FileWriter to create a file. I have an error. Directory does not exist. I think FileWriter will create a directory if it does not exist.

FileWriter writer = new FileWriter(sFileName);
+5
source share
2 answers

java.io.FileWriter does not create missing directories in the file path.

To create directories, you can do the following:

final File file = new File(sFileName);
final File parent_directory = file.getParentFile();

if (null != parent_directory)
{
    parent_directory.mkdirs();
}

FileWriter writer = new FileWriter(file);
+16
source

From the API documentation, we can conclude that FileWriter does not create a DIR if it does not exist:

Filewriter

public FileWriter (String fileName)
      throws IOException

Creates a FileWriter object with the file name.

Parameters:
   fileName - String The system file name.

:
   IOException - , , , , - .

+1

All Articles