File.createNewFile() thowing IOException No such file or directory

file-io, java

Solution

normally this is something you changed recently, first off your sample code is if not file exists and not create new file - you are trying to code away something - what is it?

Then, look at a directory listing to see if it actually exists and do a println / toString() on the file object and getMessage() on the exception, as well as print stack trace.

Then, start from zero knowledge again and re factor from the get-go each step you are using to get here. It's probably a duh you stuck in there somewhere while conceptualizing in code ( because it was working ) - you just retrace each step in detail, you will find it.

Problem

I have a method that writes to a log file. If the file exists it should append to it, if not then I want it to create a new file. ``` if (!file.exists() && !file.createNewFile()) { System.err.println("Error with output file: " + outFile + "\nCannot create new file."); continue; } ``` I have that to check that a file can be created. file is a java.io.File object. createNewFile is throwing an IOException: No such file or directory. This method has been working perfectly since I wrote it a few weeks ago and has only recently starting doing this although I don't know what I could have changed. I have checked, the directory exists and I have write permissions for it, but then I thought it should just return false if it can't make the file for any reason. Is there anything that I am missing to get this working?

Original source