gpt4 book ai didi

java - 如何解决系统找不到指定文件的错误?

转载 作者:行者123 更新时间:2023-12-01 19:08:15 26 4
gpt4 key购买 nike

对于一个项目,我需要读取包含 pokemon 数据的 csv 文件,我尝试创建一个方法,允许我在 csv 文件中查找数据,如下所示:

private final String fileName = "./data/pokedex.csv";
String line = "";
private final String separator = ",";
private List<String> getPokeInfoById(int id) {
int i = 0;
List<String> info = null;
BufferedReader br;
try {
br = new BufferedReader(new FileReader(fileName));
while ((line = br.readLine()) != null) {
if (i == id) {
try (Stream<String> stream = Arrays.stream(line.split(","))) {
info = stream.collect(Collectors.toList());
}
} else {
i++;
}
}
} catch (IOException e) {
e.printStackTrace();
}

return info;
}

但是java说:

java.io.FileNotFoundException: data\pokedex.csv (The system cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:213)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:155)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:110)
[...]

这是我的 src 的样子:

    C:.
├───controleur
├───data
│ grid_types.csv
│ moves.csv
│ pokedex.csv

├───img
├───model
│ │ Pokemon.java
│ │ Type.java
│ │
│ └───analyzer
│ Pokedex.java

└───test
PokedexTest.java

最佳答案

尝试在文件名中提供完整路径

关于java - 如何解决系统找不到指定文件的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59511671/

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