I use FileWriter for create a file. I have an error Directory does not exist I think that FileWriter create the directory if it did not exist
FileWriter writer = new FileWriter(sFileName);
I use FileWriter for create a file. I have an error Directory does not exist I think that FileWriter create the directory if it did not exist
FileWriter writer = new FileWriter(sFileName);
java.io.FileWriter
does not create missing directories in the file path.
To create the directories you could 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);
&& !parent_directory.exists()
to the if
check? –
Grandparent mkdirs()
only returns true
it if it created the directory, which implies it performs an existence check itself. –
Partlow 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 IOExceptionConstructs a FileWriter object given a file name.
Parameters:
fileName
- String The system-dependent filename.Throws:
IOException
- if the named file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason
© 2022 - 2024 — McMap. All rights reserved.