简体   繁体   English

Scanner.hasNext()返回false

[英]Scanner.hasNext() returns false

I have directory with many files in it - each with over 800 lines in it. 我有一个包含许多文件的目录 - 每个文件都有超过800行。 Hovewer, when I try to read it using Scanner, it seems empty. Hovewer,当我尝试使用Scanner阅读它时,它似乎是空的。

File f1 = new File("data/cityDistances/a.txt"),
     f2 = new File("data/cityDistances/b.txt");
System.out.println(f1.exists() && f2.exists()); //return true
System.out.println(f1.getTotalSpace() > 0 && f2.getTotalSpace() > 0); //return true
Scanner in = new Scanner(f1);
System.out.println(in.hasNext()); // return false;
System.out.println(in.hasNextLine()); //return false;

Why can it behave like that? 为什么它会像那样?


I've managed to do it using BufferedReader . 我已经设法使用BufferedReader Nonetheless, it seems even more strange that BufferedReader works and Scanner didn't. 尽管如此, BufferedReader工作原理和Scanner工作原理似乎更加奇怪。

As the default delimeter for Scanner is whitespace, that would imply your a.txt contains only whitespace - does it have 800 lines of whitespace? 由于Scanner的默认分隔符是空格,这意味着你的a.txt只包含空格 - 它有800行空白吗? ;) ;)

Have you tried the following? 你试过以下吗?

new Scanner(new BufferedReader(new FileReader("a.txt")));

I had a similar problem today reading a file with Scanner. 今天我在使用Scanner阅读文件时遇到了类似的问题。 I specified the encoding type of the file and it solved the problem. 我指定了文件的编码类型,它解决了问题。

scan = new Scanner(selectedFile,"ISO-8859-1");

This also happened to me today. 这也发生在我今天。 I'm reading a plain text file from a Linux system written by some application in a Windows box and Scanner.hasNextLine() is always false even tough there are lines with the Windows line separator and all. 我正在从一个Windows系统中的某个应用程序编写的Linux系统中读取一个纯文本文件, Scanner.hasNextLine()总是假的,甚至很难有Windows行分隔符和所有行。 As said by Hound Dog, by using 正如猎犬所说,通过使用

Scanner scanner = new Scanner(new BufferedReader(new FileReader(file))); 

it worked like a charm. 它就像一个魅力。 FileReader or BufferedReader seem to properly identify and use some file characteristics. FileReader或BufferedReader似乎正确识别并使用某些文件特征。

Checking the scanner's exception may show that the file can't be read. 检查扫描仪的异常可能表示无法读取文件。

...
System.out.println(in.hasNext()); // return false;
IOException ex = in.ioException();
if (ex != null)
  ex.printStackTrace(System.out);
...

This probably doesn't answer the OP's question, but for anyone else who is experiencing "my iterator's hasNext() is always false!"--if you have a bunch of watches with .next() in them, your IDE or whatever is actually advancing the cursor position per each one. 这可能不能回答OP的问题,但对于其他遇到“我的迭代器的hasNext()总是假的人来说!” - 如果你有一堆带有.next()的手表,你的IDE或其他什么是实际上每个推进光标位置。 This has caused me much trouble. 这给我带来了很多麻烦。 Be careful with iterators and watches. 小心迭代器和手表。

The function File.getTotalSpace() is not behaving how you're expecting. File.getTotalSpace()函数的行为与您的预期不符。 It is returning the size of the partition where those particular files are located. 它返回这些特定文件所在分区的大小。

You want to use File.length(). 您想使用File.length()。

refer to file address you are using linux, refer to your name there is possible some Polish or Czech character and refer to below link, scanner dont like non utf-8 characters on linux:) 请参考您使用linux的文件地址,请参考您的名字,可能有一些波兰语或捷克语字符,请参阅下面的链接,扫描仪不喜欢linux上的非utf-8字符:)

http://karussell.wordpress.com/2008/09/04/encoding-issues-solutions-for-linux-and-within-java-apps/ http://karussell.wordpress.com/2008/09/04/encoding-issues-solutions-for-linux-and-within-java-apps/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM