Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中读取文件时发生编译错误_Java_File - Fatal编程技术网

在Java中读取文件时发生编译错误

在Java中读取文件时发生编译错误,java,file,Java,File,我试着运行这个简单的程序,从一个单独的文本文件中读取并打印出每一行。然而,当我试图编译它时,它总是给我同样的错误: story.java:11: error: unreported exception FileNotFoundException; must be caught or declared to be thrown x = new Scanner(new File("names.txt")); 这是我的密码: import java.io.*; imp

我试着运行这个简单的程序,从一个单独的文本文件中读取并打印出每一行。然而,当我试图编译它时,它总是给我同样的错误:

story.java:11: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
                x = new Scanner(new File("names.txt"));
这是我的密码:

import java.io.*;
import java.util.*;

public class story {

    private static Scanner x;

    public static void main(String[] args) {

        String story = "";
        x = new Scanner(new File("names.txt"));

        while(x.hasNext()){
            story = story + x;
        }
        System.out.println(story);

    }
}

此消息告诉您,您的
main()
方法正在执行一些可能引发
FileNotFoundException
的操作,但您既没有捕获此异常,也没有声明此类异常可能由
main()
函数引发

要更正它,请按如下方式声明
main()
方法:

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

编程时,有解决问题的正常逻辑流,还有处理“异常”意外情况的第二个逻辑流。Java使用类型
Exception
和关键字
throw
使代码位呈现异常错误状态

您的程序在编译时,正在检查其处理异常返回值的能力。由于您没有处理异常返回值,因此不会对其进行编译

下面是处理此异常的代码。请密切注意
try
catch
块结构,因为它是一个人如何编写代码来处理引发的
异常。但是,不要被我为您处理异常的方式所限制,因为如何处理异常的细节取决于您喜欢做什么

import java.io.*;
import java.util.*;

public class story {

    private static Scanner x;

    public static void main(String[] args) {

        String story = "";
        try {
            x = new Scanner(new File("names.txt"));
        } catch (FileNotFoundException error) {
            System.out.println("the program failed because \"names.txt\" was not found.");
            return -1;
        }

        while(x.hasNext()){
            story = story + x;
        }
        System.out.println(story);

    }
}
还有另一种处理异常的方法有时是合适的,即“不处理”异常。执行此操作时,请将方法从
无论该方法是什么
更改为
无论该方法是什么都会引发异常
。例如,
publicstaticvoidmain(String[]args)
publicstaticvoidmain(String[]args)抛出异常

虽然这可以处理编译器的投诉,但应该谨慎使用,因为通过嵌套方法调用而冒出的异常经过
main
方法会使程序崩溃,通常没有易于读取的输出,这可能会使错误更容易被人修复。

将其更改为

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

    String story = "";
    x = new Scanner(new File("names.txt"));

    while(x.hasNext()){
        story = story + x;
    }
    System.out.println(story);

}

在代码中加入try-catch

public static void main(String[] args) {

    String story = "";
    try {
        x = new Scanner(new File("names.txt"));
        while(x.hasNext()){
            story = story + x;
        }
        System.out.println(story);
    }
    catch(Exception e) {
        // handle 
    }
}

无论何时处理文件对象,都必须处理FileNotFoundException的可能性,当指定的文件不存在时会发生这种情况

要解决这个问题,只需在主标题中说
抛出异常
,或者使用try/catch块

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

1) 你需要把它们放在试抓块里。 2) 指定“names.txt”的路径。 3) 我的代码如下所示:

String story = "";
       try {
         x = new Scanner(new File("/Users/Workspace/names.txt"));
         while(x.hasNext()){
            System.out.println(story = story+x.next());
         }
         System.out.println(story);
     } catch (FileNotFoundException e){
         e.printStackTrace();
     }
 }

消息很清楚:未报告的异常FileNotFoundException;必须被抓住,否则。。。您需要使用try-catch或try-with-resources来处理文件…或者
抛出FileNotFoundException
您应该了解
String story = "";
       try {
         x = new Scanner(new File("/Users/Workspace/names.txt"));
         while(x.hasNext()){
            System.out.println(story = story+x.next());
         }
         System.out.println(story);
     } catch (FileNotFoundException e){
         e.printStackTrace();
     }
 }