I am trying to write a test file that will overwrite the file I am working on so I can use it with a much more complex program. I keep getting an error message associated with creating a new PrintWriter.
This is the error message:
unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown PrintWriter printWriter = new PrintWriter(file);
This is my code:
import java.io.PrintWriter;
import java.io.File;
public class rewriting_test_file {
public static void main(String[] args) {
File file = new File ("C:/Users/XXXXXXX/Desktop/Java practice/rewriting_test_file.java");
file.getParentFile().mkdirs();
PrintWriter printWriter = new PrintWriter(file);
printWriter.println ("hello");
printWriter.close ();
}
}