Scanner.hasNext()返回false [英] Scanner.hasNext() returns false

查看:316
本文介绍了Scanner.hasNext()返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目录中包含许多文件 - 每个文件都包含800多行。 Hovewer,当我尝试使用Scanner读取它时,它似乎是空的。

I have directory with many files in it - each with over 800 lines in it. Hovewer, when I try to read it using Scanner, it seems empty.

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;

为什么它会像那样?

我已经设法使用 BufferedReader 来完成。但是,似乎更奇怪的是 BufferedReader 有效,扫描器没有。

I've managed to do it using BufferedReader. Nonetheless, it seems even more strange that BufferedReader works and Scanner didn't.

推荐答案

由于Scanner的默认分隔符是空白,这意味着您的 a.txt 仅包含空格 - 它有800行空白吗? ;)

As the default delimeter for Scanner is whitespace, that would imply your a.txt contains only whitespace - does it have 800 lines of whitespace? ;)

您是否尝试过以下操作?

Have you tried the following?

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

这篇关于Scanner.hasNext()返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆