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

时间:2019-12-28 14:14:06

标签: java csv bufferedreader

对于一个项目,我需要读取一个包含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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

所以我的路径似乎是错误的,因为我没有注意当前目录的使用情况

System.out.println(new File("."). getAbsolutePath());

帮助我找到自己的位置并找到正确的路径