我在使用以下代码时遇到问题。我正在尝试写入 .ppm 文件,并且得到
Red.java:6: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown FileOutputStream fout = new FileOutputStream(fileName); ^ Any ideas?
导入java.io.*;
公开课红色{
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();
}
}
最佳答案
最简单的解决方案是重写您的主要声明:
public static void main(String args[]) throws FileNotFoundException {...
这表明如果它无法创建输出流(无论出于何种原因),它可能抛出此异常。请注意,在这种情况下,FileNotFoundException 不是异常的最佳名称,但这是一个您无法处理的命名问题。
事实上,您可能希望在上面的 main()
throws 子句中声明 IOException
。您调用的不同方法将被声明为该方法的抛出变体。
关于java - FileOutputStream 文件未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/729283/