Constructors of FileWriter classCreates a new file. It gets file name in string.
FileWriter(File file) : Creates a FileWriter object using specified File object. It throws an IOException if the file exists but is a directory rather than a regular file or does not exist but cannot be created, or cannot be opened for any other reason.
The FileWriter maintains the file's position and length attributes, so you can seek and write anywhere in the file. By default, the FileWriter writes to the beginning of the file (will overwrite existing data).
So I have a code snippet as follows. Im trying to find out why it throws a FileNotFoundException.
File file= new File (WORKSPACE_PATH+fname);
FileWriter fw;
if (file.exists())
{
fw = new FileWriter(file,true);//if file exists append to file. Works fine.
}
else
{
fw = new FileWriter(file);// If file does not exist. Create it. This throws a FileNotFoundException. Why?
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With