我正在测试csv文件读取,并且不断收到相同的错误。显示的错误是“无法读取请求的文件。原因: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)
答案 0 :(得分:2)
在Java中,Exception
是Throwable
的子类。 Throwable.getCause()
方法返回“导致抛出该throwable的throwable”(根据Javadoc)。通常可以是null
。
相反,调用Throwable.getMessage()
,这是“ [r]返回此可抛出对象的详细消息字符串”。
答案 1 :(得分:1)
我将System.err.println("Could not read the requested file. Cause: " + e.getCause());
替换为e.printStackTrace()
以得到此信息:
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);