为更多读者编辑:问题是我的输入文件已损坏.
我不明白我做错了什么:
我使用的是这段代码:
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); } }
哪个工作正常.现在,出于某种原因,我想换一台扫描仪.我的代码成了:
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
此代码逐行读取文件.
public static void readFileByLine(String fileName) { try { File file = new File(fileName); Scanner scanner = new Scanner(file); while (scanner.hasNext()) { System.out.println(scanner.next()); } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
您还可以将分隔符设置为行分隔符,然后执行相同的操作.
scanner.useDelimiter(System.getProperty("line.separator"));
您必须检查是否有可用的下一个令牌,然后读取下一个令牌.您还需要双重检查给予扫描仪的输入.即dico.txt.默认情况下,Scanner根据空格中断输入.请确保输入的分隔符位于正确的位置
更新后的答案:
我刚刚尝试使用如下内容创建输入文件
a à abaissa abaissable abaissables abaissai abaissaient abaissais abaissait
试着用下面的代码阅读它.刚刚工作正常.
File file = new File("/home/keerthivasan/Desktop/input.txt"); Scanner scr = null; try { scr = new Scanner(file); while(scr.hasNext()){ System.out.println("line : "+scr.next()); } } catch (FileNotFoundException ex) { Logger.getLogger(ScannerTest.class.getName()).log(Level.SEVERE, null, ex); }
输出:
line : a line : à line : abaissa line : abaissable line : abaissables line : abaissai line : abaissaient line : abaissais line : abaissait
所以,我相信这应该有效.由于您在Windows环境中工作,行结束(EOL)序列(0x0D 0x0A,\ r \n)实际上是两个ASCII字符,CR和LF字符的组合.如果您将Scanner实例设置为使用分隔符,如下所示,它可能会提取
scr = new Scanner(file); scr.useDelimiter("\r\n");
然后循环读取行.希望这可以帮助!
next()和nextLine()方法与Scanner关联,用于获取String输入.他们的差异是......
next()只能读取输入直到空格.它无法读取由空格分隔的两个单词.此外,next()在读取输入后将光标置于同一行.
nextLine()读取包含单词之间空格的输入(即,它读取直到行的结尾\n).读取输入后,nextLine()将光标定位在下一行.
阅读文章:next()和nextLine()之间的区别
用以下内容替换while循环:
while(r.hasNext()) { scan = r.next(); System.out.println(scan); if(scan.length()==0) {continue;} //treatment }
使用hasNext()
和next()
方法将解决问题.