为什么我会收到文件未找到异常?
Why am I getting file not found exception?
我通过 eclipse 将文本文件导入到我的 java 项目文件夹中。我正在尝试加载包含随机单词字典的文本,遍历它们并创建一个哈希图,其中单词的第一个字母是键,整个单词是值。
我在 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();
}
}
}
}
我在 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"));
}
}
异常:
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
要访问项目结构中的文件,您需要re-create从项目根目录到文件本身的路径。
假设文件位于 src
文件夹中,例如
PROJECT
|
src
|
nouns.txt
aPackage
|
Main.java
那么下面的代码就成功了:
public class Main {
public static void main(String[] args) {
System.out.println(Files.exists(Paths.get("src", "nouns.txt")));
}
}
我总是使用 InputStream
,而不是从 File
得到 BufferedReader
。例如,您可以这样做:
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();
}
当然,这里的路径假定您的 txt
文件在 src
下,而不是一个包,但您可以随时更改它。您甚至可以创建第二个源文件夹并将其放入其中。
我通过 eclipse 将文本文件导入到我的 java 项目文件夹中。我正在尝试加载包含随机单词字典的文本,遍历它们并创建一个哈希图,其中单词的第一个字母是键,整个单词是值。
我在 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();
}
}
}
}
我在 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"));
}
}
异常:
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
要访问项目结构中的文件,您需要re-create从项目根目录到文件本身的路径。
假设文件位于 src
文件夹中,例如
PROJECT
|
src
|
nouns.txt
aPackage
|
Main.java
那么下面的代码就成功了:
public class Main {
public static void main(String[] args) {
System.out.println(Files.exists(Paths.get("src", "nouns.txt")));
}
}
我总是使用 InputStream
,而不是从 File
得到 BufferedReader
。例如,您可以这样做:
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();
}
当然,这里的路径假定您的 txt
文件在 src
下,而不是一个包,但您可以随时更改它。您甚至可以创建第二个源文件夹并将其放入其中。