如何修复读取 csv 文件时 getCause 指向 null 的 FileReader 错误?

How to fix FileReader error where getCause points to null when reading csv file?

我正在测试 csv 文件读取,但我一直收到同样的错误。显示的错误是 "Could not read the requested file. Cause: null"。这是我的代码的简化版本,它仍然会产生相同的错误。

public class PersonReader {
    public static List<Person> readPersons(String path) throws IOException{
        ArrayList<Person> persons = new ArrayList<>();
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(path));
            String line = null; // read first line

            while((line = reader.readLine()) != null) {
                persons.add(new Person(line));
            }
        } finally {
            if(reader != null) {
                reader.close();
            }
        }

        return persons;
}

public class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

public class Main{
    public static void main(String[] args) {
        List<Person> persons = null;

        try {
            persons = PersonReader.readPersons("sample/Data.csv");
        } catch (IOException e) {
            System.err.println("Could not read the requested file. Cause: " + e.getCause());
        }

        if(persons == null) { // some error has occurred
            System.exit(1);
        }

        System.out.println(persons);
    }
}

我试过的 PersonReader 的替代版本产生了同样的错误

public class PersonReader {
    public static List<Person> readPersons(String path) throws IOException{
        ArrayList<Person> persons = new ArrayList<>();
        BufferedReader reader = null;

        try {
            reader = Files.newBufferedReader(Paths.get(path));
            String line = null;

            while((line=reader.readLine()) != null) {
                persons.add(new Person(line));
            }
        } finally {
            if(reader != null) {
                reader.close();
            }
        }

        return persons;
    }

已生成 StackTrace

java.io.FileNotFoundException:   (The system cannot find the path specified)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
    at java.base/java.io.FileReader.<init>(FileReader.java:60)
    at sample.PersonReader.readPersons(PersonReader.java:17)
    at sample.Main.main(Main.java:14)

我用 e.printStackTrace() 替换了 System.err.println("Could not read the requested file. Cause: " + e.getCause()); 得到这个:

java.io.FileNotFoundException: sample/Data.csv (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at java.io.FileReader.<init>(FileReader.java:58)
    at xxx.yyy.zzz.PersonReader.readPersons(A.java:37)
    at xxx.yyy.zzz.A.main(A.java:73)

问题是 FileReader 无法使用其相对路径找到文件 - 没有相对于当前工作目录的文件 "sample/Data.csv"。您可以提供绝对路径,或者如果您可以将文件添加到您的项目资源中,您可以这样做:

  InputStream inputStream = PersonReader.class.getClassLoader().getResourceAsStream("sample/Data.csv");
  InputStreamReader streamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
  BufferedReader reader = new BufferedReader(streamReader);

在 Java 中,ExceptionThrowable 的子 class。 Throwable.getCause() 方法 returns "the throwable that caused this throwable to get thrown" (根据 Java 文档)。这通常可以是 null.

相反,调用 Throwable.getMessage(),这“[r]返回此 throwable 的详细消息字符串”。