简体   繁体   中英

Why am I getting file not found exception?

I have text files imported into my java project folder through eclipse. I am trying to load up the texts which contains a dictionary of random words, go through them and create a hashmap with the first letter of the words being the key and the word as a whole being the value.

I have a method in a class WordStore:

public WordStore(String k) throws IOException {
    map = new HashMap<String, List<String>>();
    BufferedReader buffread = null;
    File filename = null;
    FileReader fread = null;
    try{
        filename = new File(k);
        fread = new FileReader(filename);
        buffread = new BufferedReader(fread);   
        String word ="";

        while((word = buffread.readLine()) != null) {
            if(word.length()<3) {
            //don't add word less than 3 characters long
            }
            else {
                String key = ""+(word.charAt(0));
                put(key, word);
            }

        }

    }
    catch(IOException e) {
        System.out.println("File not found exception caught!");
    }
    finally {
        if(buffread != null) {
            try {
                buffread.close();
            }
        catch(IOException e) {
            e.printStackTrace();
        }
        }
    }

}

I use it in the class WordStoreTest:

import java.io.IOException;

public class WordStoreTest {

public static void main(String[] args) throws IOException {
    WordStore store = new WordStore("nouns.txt");
    System.out.println(store.getRandomWord("b"));

}

}

Exception:

File not found exception caught! java.io.FileNotFoundException: nouns.txt (No such file or directory
 at java.io.FileInputStream.open0(Native Method
 at java.io.FileInputStream.open(FileInputStream.java:195
 at java.io.FileInputStream.(FileInputStream.java:138
 at java.io.FileReader.(FileReader.java:72
 at WordStore.(WordStore.java:34
 at WordStoreTest.main(WordStoreTest.java:14) null

在此处输入图片说明

To access a file that is inside your project structure, you need to re-create the path from the project root to the file itself.

Assuming the file resides in the src folder, like

PROJECT
 |
  src
   |
    nouns.txt
    aPackage
      |
       Main.java

Then following code is successful:

public class Main {
  public static void main(String[] args) {
     System.out.println(Files.exists(Paths.get("src", "nouns.txt")));
 }
}

Instead of getting a BufferedReader from a File , I always use an InputStream . As an example, you could do this:

String path = "/nouns.txt";
try {
    InputStream is = this.getClass().getResourceAsStream("/misc/sample.txt");
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));


    String line = bufferedReader.readLine();
    for(; line!=null; line=bufferedReader.readLine()) {
        System.out.println(line);
    }


    bufferedReader.close();
} catch (IOException e) {
    e.printStackTrace();
}

Of course, the path here assumes that your txt files are under src and not a package, but you could always change that. You could even create a second source folder and put it inside of there.

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