java - 如何解决系统找不到指定文件的错误?
问题描述
对于一个项目,我需要读取一个包含 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
解决方案
所以看来我的路径是错误的,因为我没有注意我当前的目录正在使用什么
System.out.println(new File("."). getAbsolutePath());
帮助我找到了自己并找到了正确的道路
推荐阅读
- typescript - 打字稿说属性推送不适用于“位置 [] | Thenable
" - .net-core - 如何使用私人密钥和 rsasha256 算法 (.net) 创建 jwt 签名?
- mysql - 如何在 SQL 模式中正确设计 2 个表?
- python - 如何生成点分隔字符串的子字符串?
- javascript - 使用 vanilla Javascript 构建购物车功能
- intellij-idea - 如何在 Intellij 中重置“如果由 Y 注释,则抑制 X 警告”?
- docker - 如何在 Pycharm Docker Interpreter 中设置 docker 容器名称
- c++ - 混合虚拟和模板
- r - 同时创建多个子集
- azure - Azure Key Vault 扩展在更新后未自动绑定