@VGR 做对了。
tl;博士:使用Scanner in = new Scanner(new File(fileName), "ISO-8859-1");
似乎正在发生的是:
- 由于那个单独的 0x9C 字符,您的文件不是有效的 UTF-8。
- 扫描仪正在以 UTF-8 格式读取文件,因为这是系统默认设置
- 底层库抛出一个
MalformedInputException
- 扫描仪捕捉并隐藏它(一个善意但被误导的设计决定)
- 它开始报告它没有更多的行
- 除非你真的问扫描仪,否则你不会知道有什么问题
这是一个 MCVE:
import java.io.*;
import java.util.*;
class Test {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(new File(args[0]), args[1]);
while (in.hasNextLine()) {
String line = in.nextLine();
System.out.println("Line: " + line);
}
System.out.println("Exception if any: " + in.ioException());
}
}
这是一个正常调用的示例:
$ printf 'Hello\nWorld\n' > myfile && java Test myfile UTF-8
Line: Hello
Line: World
Exception if any: null
这是您所看到的(除了您没有检索并显示隐藏的异常)。请特别注意,没有显示任何行:
$ printf 'Hello\nWorld \234\n' > myfile && java Test myfile UTF-8
Exception if any: java.nio.charset.MalformedInputException: Input length = 1
这是当解码为 ISO-8859-1 时,所有字节序列都有效的解码(即使 0x9C 没有分配的字符,因此不会显示在终端中):
$ printf 'Hello\nWorld \234\n' > myfile && java Test myfile ISO-8859-1
Line: Hello
Line: World
Exception if any: null
如果您只对 ASCII 数据感兴趣并且没有任何 UTF-8 字符串,您可以ISO-8859-1
通过将其作为第二个参数传递给Scanner
构造函数来简单地要求扫描仪使用:
Scanner in = new Scanner(new File(fileName), "ISO-8859-1");