编辑:针对后来的读者,问题是我的输入文件已损坏。
我不明白我做错了什么:
我正在使用这段代码:
File f = new File("C:\\Temp\\dico.txt");
BufferedReader r = null;
try {
r = new BufferedReader(new FileReader(f));
String scan;
while((scan=r.readLine())!=null) {
if(scan.length()==0) {continue;}
//treatment
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(r!=null) try {
r.close();
} catch (IOException ex) {
Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex);
}
}
现在这段代码已经很好地运作了。不过,出于某种原因,我希望更换一个Scanner。我的代码变成了:
File f = new File("C:\\Temp\\dico.txt");
Scanner r = null;
try {
r = new Scanner(f);
String scan;
while(r.hasNextLine()) {
scan = r.nextLine();
if(scan.length()==0) {continue;}
//treatment
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(r!=null) r.close();
}
这一次,我们从未进入 while 循环,因为 r.hasNextLine() 总是返回“false”。你有什么想法,我错在哪里了吗?
我要明确的是,除此之外没有任何变化,文件仍然是原来的那个。
编辑:我也要明确的是,我尝试使用另一个文件得到了相同的结果,这意味着问题似乎不是来自文件。
文件长成这样:
a
à
abaissa
abaissable
abaissables
abaissai
abaissaient
abaissais
abaissait
...
编辑2:
由于问题只在我将文件内容从我的文件复制/粘贴到另一个文件时才出现,因此文件的内容似乎存在问题。换句话说,如果我自己编写内容,则可以正常工作;如果我使用dico.txt文件中的一部分内容,则无法正常工作。有什么解释吗?
编辑3:
这些是我的文件链接。我建议您避免使用非常庞大的dico.txt。dico.txt:https://drive.google.com/file/d/0B0sroFy9HZlBNDl3MUwzVnh6VU0/edit?usp=sharing test.txt:https://drive.google.com/file/d/0B0sroFy9HZlBemZjbXU1RmlmdjQ/edit?usp=sharing
dico.txt
文件吗? - Alexis C.