Why is FileWriter not creating a new file ? FileNotFoundException

seeker picture seeker · Aug 13, 2014 · Viewed 36.3k times · Source

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

Answer

nbz picture nbz · Aug 13, 2014

Using concatenation when creating the File won't add the necessary path separator.

File file = new File(WORKSPACE_PATH, fname);