从 Java 输入流中读取下一个字符(完整的 Unicode 代码点)

mrp*_*pyo 5 java utf-8

我需要逐个字符地解析 UTF-8 输入(来自文本文件)(按字符我的意思是完整的 UTF-8 字符(UTF-8 代码点),而不是 Java 的字符)。

我应该使用什么方法?

Are*_*rff 2

CharSequence.codePoints()

例如:

String text = Files.readString(Path.of("test.txt"));

IntStream codePoints = text.codePoints();

// do something with the code points
codePoints.forEach(codePoint -> System.out.println(codePoint));
Run Code Online (Sandbox Code Playgroud)

  • 是的,如果它是一个流怎么办 - 如何逐个代码点读取代码点而不是首先将整个文件变成字符串? (4认同)