Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is FileWriter not creating a new file ? FileNotFoundException [duplicate]

Tags:

People also ask

Does FileWriter create a new file?

Constructors of FileWriter classCreates a new file. It gets file name in string.

Does FileWriter create the file if it does not exist?

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.

Does FileWriter overwrite existing file?

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? 
}