简体   繁体   中英

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. 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. 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. Also, how can I close the files? f.close() gives me an error.

The file in your src or .class directory, could only mean that the file is in classpath, while new File(filename); search the current working directory. To search classpath, change readDictionary method, after File f = new File(filename); 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. Should work also:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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