文件在同一个文件夹中时找不到Java文件错误

问题描述 投票:0回答:2

enter image description here

这是我读取文件的代码:

public Dictionary(){
        wordList = new ArrayList<String>(1300);
        dictArrayList = new ArrayList<>(1300);
        readFile("ionDictionary.txt");
    }


    public Dictionary(String filename) {
        wordList = new ArrayList<>(1300);
        dictArrayList = new ArrayList<>(1300);
        readFile(filename);
    }

    public void readFile(String filename){
        try {
            Scanner scanner = new Scanner(new File(filename));
            while (scanner.hasNextLine()) {
                splitStoreLine(scanner);
            }
            scanner.close();
            Collections.sort(wordList);
        } catch (FileNotFoundException e) {
            System.out.println("File not found.");
        }
    }

我试着检查路径,它是一样的。

java filenotfoundexception
2个回答
0
投票

您可能正在从项目的根目录执行代码,请尝试将文件的路径更改为

src/H/HW2/ionDictionary.txt


0
投票

你似乎在使用 ItelliJIdea,你的文件应该有一个相对路径到运行配置中的“工作目录”

通常工作目录是项目的路径,在你的情况下

~/Desktop/CS258/

© www.soinside.com 2019 - 2024. All rights reserved.