I've read several threads on here and still can't find out why PrintWriter isn't printing to file and is instead throwing a java.io.FileNotFoundException. I've checked the file location, I've checked the upper/lowercases in the file path. Can someone tell me what I'm doing wrong:
File output = new File("/Users/<myname>/Documents/javaoutput.txt");
output.getParentFile().mkdir();
PrintWriter writer = new Printwriter(output);
You're not checking whether the target directory
/Users/<myname>/Documentsexists or whether the attempt to create it succeeded.new PrintWriter()would throw aFileNotFoundExceptionif the target directory didn't exist.Something like this might work better:
I'm using
mkdirs()here instead ofmkdir()to create the entire directory path (/Users,/Users/<myname>, and/Users/<myname>/Documents) if necessary. You should decide what your program should do when it couldn't create the directory and add the correct code for that case.