gpt4 book ai didi

java - 调用方法时出现 IOException 错误

转载 作者:行者123 更新时间:2023-12-02 07:40:37 28 4
gpt4 key购买 nike

我正在尝试写入文件。

我已经在 writeHtmlFile 方法中声明了异常,但是当我尝试调用 writeHtmlFile 方法时,错误“必须捕获或声明抛出未报告的异常 java.io.IOException”仍然出现?

public class PartB extends ChangeDrawer
{

public static ChangeDrawer cd = new ChangeDrawer();
static int[] floatDrawer = {8,5,4,4,5,20,20,6,10,3,8};

{
String selection="";
Scanner scan = new Scanner (System.in);

System.out.println ("Enter P to make a purchase and receive your change");
System.out.println ("Enter L to load the Change drawer");
System.out.println ("Enter H to write the contents of the Change Drawer to a web page");
System.out.println ("Enter E to exit the program");


while (selection.compareTo("E")!=0)
{
selection = scan.next();
if (selection.compareTo("P")== 0)
makeChange();
else if (selection.compareTo("L")==0)
loadFloat();
else if (selection.compareTo("H")==0)
writeHtmlFile(); //unreported exception java.io.IOException must be caught or
//declared to be thrown


}
System.out.println ("Ending .............................. ");
}


//more code exists between these two sets

public static void writeHtmlFile() throws IOException
{
FileWriter fwriter = new FileWriter("ChangeDrawer.html");
PrintWriter outputFile = new PrintWriter(fwriter);
outputFile.println("This should work!");

}

最佳答案

调用 writeHtmlFile 的代码必须捕获(或重新声明抛出)IOException。由于调用代码位于静态初始化程序中,因此它必须是前者。

关于java - 调用方法时出现 IOException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11662048/

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