[英]Why am I getting file not found exception?
I have text files imported into my java project folder through eclipse.我通过 eclipse 将文本文件导入到我的 java 项目文件夹中。 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:我在 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:我在 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假设文件位于
src
文件夹中,例如
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
.我总是使用
InputStream
,而不是从File
获取BufferedReader
。 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.当然,这里的路径假定您的
txt
文件在src
下而不是包下,但您可以随时更改它。 You could even create a second source folder and put it inside of there.您甚至可以创建第二个源文件夹并将其放入其中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.