RT,很不明白
File file = new File("ChineseDictionary.dat");
System.out.println("文件存在么?");
System.out.println(file.exists());
//下面的if语句给dic初始化
if (file.exists()) {
dic = dic.loadDic("ChineseDictioanry.dat");
} else {
dic.loadDic(file);
}
File file = new File("ChineseDictionary.dat");
System.out.println("文件存在么?");
System.out.println(file.exists());
//下面的if语句给dic初始化
if (file.exists()) {
dic = dic.loadDic("ChineseDictioanry.dat");
} else {
dic.loadDic(file);
}
File file = new File(URLDecoder.decode(Main.class.getResource("").getPath(), "UTF-8")+"ChineseDictionary.dat");
换成这样看看
强烈怀疑放置路径问题。解决方法:
1.把ChineseDictionary.dat文件备份,然后删掉
2.运行下述代码:File file = new File("ChineseDictionary.dat");
file.createNewFile();3.查找新生成的ChineseDictionary.dat文件在哪个路径下。
try {
FileReader fr = new FileReader(new File(Test.class.getResource("").getPath()+"/201001.txt"));
BufferedReader br = new BufferedReader(fr);
String str;
// while((str = br.readLine())!=null){
// System.out.println(str);
// }
} catch (FileNotFoundException e) {//捕获异常
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}像这样,如果文件不存在就会打印堆栈信息,对出错的地方快速定位
下面把file的getAbsolutePath()打出拉看看是什么就扔那里去,(单词可能拼错了,用IDE多了的下场
楼主在9楼已经说过,那个文件就应该在项目文件夹的。我也用eclipse验证过了。我觉得问题出在别处。比如,我不太明白楼主那个dic初始化的代码。另外,你到底用的什么开发环境?我建议楼主把变量名改一下,这样
File dicFile = new File("ChineseDictionary.dat");
System.out.println("文件存在么?");
System.out.println(dicFile.exists()); 再打印不出 true ,楼主就去跳楼吧。另,多谢10楼帮我贴exception的代码了。不过我又想了一下,可能在这里用不上。
File file = new File("ChineseDictionary.dat");
得到的文件路径是 项目路径/ChineseDictionary.dat