简体   繁体   English

Java Scanner hasNextLine返回false

[英]Java Scanner hasNextLine returns false

I have several files (actually they are also java source files saved in Eclipse on Ubuntu) which I need to read and process line by line. 我有几个文件(实际上它们也是在Ubuntu上保存在Eclipse中的java源文件),我需要逐行阅读和处理。 I've noticed that I cannot read one of the files. 我注意到我无法读取其中一个文件。 The code I am using is as below 我使用的代码如下

try (Scanner scanner = new Scanner(file)) {
    while (scanner.hasNextLine() ) {
        builder.append(scanner.nextLine()).append("\n");
    }
} catch (FileNotFoundException ex) {
    System.out.println("Error");
}

I was checking beforehand if the file exists. 我事先检查文件是否存在。 And it does. 确实如此。 I can even rename it. 我甚至可以重命名它。 But I cannot read a single line. 但我不能读一行。 hasNextLine simply returns false. hasNextLine只返回false。 (I even try hasNext). (我甚至尝试hasNext)。

At the end I take a look at the content of the file and find that there is a different looking character (which was in the comment section of java file). 最后,我看一下文件的内容,发现有一个不同的外观(在java文件的注释部分)。 It is the following character. 它是以下角色。

¸

When I delete this character, I can read the file normally. 当我删除这个字符时,我可以正常读取该文件。 However this is not acceptable. 但这是不可接受的。 What can I do to read the files even with that character in it? 如果文件中包含该字符,我该怎么做?

This is most probably a character set issue, caused by the fact that the platform you are running your java code uses by default a different set; 这很可能是一个字符集问题,因为您运行Java代码的平台默认使用不同的集合; it is always a good practice to specify the expected/needed character set to be used when parsing, and with the Scanner class is just a matter of calling the constructor as: 指定解析时要使用的预期/需要的字符集始终是一个好习惯,而使用Scanner类只需要将构造函数调用为:

Scanner scanner = new Scanner(file, "UTF-8");

where the second parameter is the character set literal, or even better : 其中第二个参数是字符集文字,甚至更好

Scanner scanner = new Scanner(file, StandardCharsets.UTF_8);

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

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