如何修复IOException

时间:2019-09-18 16:25:47

标签: java

我为此尝试了几个文件路径,但是即使文件存在,仍会得到IOException 这是我的代码:

public static void main(String[] args) {
        String [] letters = {"a", "b"};
        Object ob = new object(letters);
        ob.training(new File("C:/Users/Dell/Desktop/train.txt"));
        ob.classify(new File("C:/Users/Dell/Desktop/data.txt"), new File("C:/Users/Dell/Desktop/classify.txt"));
    }

我在训练和分类方法中使用了throws语句,但仍然会要求我在main方法上使用throws语句,而当我这样做时,它将无法运行。否则它将运行,但根本无法通过名为CodeGrade的程序的编译测试。有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

文件操作可能会抛出IOException,这些异常将被检查。 e。必须明确处理。您可以通过以下方式声明main方法:

public static void main(String[] args) throws IOException {

然后您的代码应编译。

答案 1 :(得分:1)

尝试在这里查看

What does it mean when the main method throws an exception?

但是如果您使用Windows,也许这就是您的溶胶:

new File("C://Users//Dell//Desktop//train.txt"))
相关问题