gpt4 book ai didi

java - FileOutputStream 文件未找到错误

转载 作者:行者123 更新时间:2023-12-01 16:41:55 25 4
gpt4 key购买 nike

我在使用以下代码时遇到问题。我正在尝试写入 .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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com