简体   繁体   English

始终接收FileNotFoundException,即使SRC文件夹中有文件也是如此

[英]Always receiving FileNotFoundException, even with file in SRC folder

I'm currently trying to write a program that will read in a file line by line and add each line to an arrayList. 我目前正在尝试编写一个程序,该程序将逐行读取文件并将每行添加到arrayList。 My other function is supposed to sort the items from that buffer, then write them to a text file. 我的另一个功能应该是对该缓冲区中的项目进行排序,然后将其写入文本文件。 However, I keep getting a FileNotFoundException, even when my file is sitting in the src directory, as well as the directory with my .class file. 但是,即使我的文件位于src目录以及带有.class文件的目录中,我仍会收到FileNotFoundException。 My code is as follows 我的代码如下

public static ArrayList<String> readDictionary(String filename, 
ArrayList<String> buffer) throws IOException, FileNotFoundException {
    File f = new File(filename);
    Scanner fileIn = new Scanner(f);
    //Scanner fileIn = new Scanner(new File(filename));
    boolean add = true;

    while (fileIn.hasNextLine() == true) {
        for (String s : buffer) {
            if (fileIn.nextLine().equals(s)) {
                add = false;
            }
        }
        if (add == true) {
            buffer.add(fileIn.nextLine());
        }
        add = true;
    }
    fileIn.close();
    return buffer;
}

public static void writeDictionary(String filename, ArrayList<String> buffer) throws IOException, FileNotFoundException {
    File f = new File(filename);
    Collections.sort(buffer, String.CASE_INSENSITIVE_ORDER);
    Path file = Paths.get(filename);
    Files.write(file, buffer);
}

public static void main(String[] args) throws IOException, FileNotFoundException{
    ArrayList<String> buffer = new ArrayList<>();
    readDictionary("inputtest.txt", buffer);
}

Exception in thread "main" java.io.FileNotFoundException: inputtest.txt (The system cannot find the file specified)
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.util.Scanner.<init>(Scanner.java:611)
at UltimateDictionary.readDictionary(UltimateDictionary.java:18)
at UltimateDictionary.main(UltimateDictionary.java:46)

I tested this program by setting filename equal to "inputtest.txt", and that file is sitting in my src directory with the .java file, but it still throws the error. 我通过将文件名设置为等于“ inputtest.txt”来测试了该程序,该文件与.java文件位于我的src目录中,但仍然会引发错误。 Also, how can I close the files? 另外,如何关闭文件? f.close() gives me an error. f.close()给我一个错误。

The file in your src or .class directory, could only mean that the file is in classpath, while new File(filename); src或.class目录中的文件只能表示该文件位于类路径中,而new File(filename); search the current working directory. 搜索当前工作目录。 To search classpath, change readDictionary method, after File f = new File(filename); 要搜索类路径,请在File f = new File(filename);之后更改readDictionary方法。 add: 加:

    if (!f.exists()) {
        URL url = <yourclassname>.class.getResource(filename);
        if (url != null)
            f =  new File(url.getPath());
    }

you have to use yourclassname.class because the method is static. 您必须使用yourclassname.class,因为该方法是静态的。 Should work also: 还应该工作:

if (url == null)
    url = ClassLoader.getSystemResource(filename);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM