I’m having trouble with the following code. I’m trying to write to a .ppm file, and I get
Red.java:6: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown FileOutputStream fout = new FileOutputStream(fileName); ^ Any ideas?
import java.io.*;
public class Red {
public static void main(String args[]) { String fileName = 'RedDot.ppm'; FileOutputStream fout = new FileOutputStream(fileName); DataOutputStream out = new DataOutputStream(fout); System.out.print('P6 1 1 255 '); System.out.write(255); System.out.write(0); System.out.write(0); System.out.flush(); }
}
The simplest solution is to rewrite your main declaration thus:
thus indicating that it may throw this exception if it can’t create the outputstream (for whatever reason). Note that FileNotFoundException is not the best name for the exception in this circumstance, but that’s a naming issue you can’t deal with.
In fact you will probably want to declare
IOExceptionin themain()throws clause above. The different methods you’re calling will be declared as throwing variants of this.