从 Java 输入流中读取下一个字符(完整的 Unicode 代码点) [英] Read next character (full unicode code point) from Java input stream
本文介绍了从 Java 输入流中读取下一个字符(完整的 Unicode 代码点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要逐个字符地解析 UTF-8 输入(来自文本文件)(按字符我的意思是完整的 UTF-8 字符(UTF-8 代码点),而不是 Java 的字符).
I need to parse UTF-8 input (from a text file) character by character (and by character I mean full UTF-8 character (UTF-8 code point), not Java's char).
我应该使用什么方法?
推荐答案
从 Java 8 开始就有 CharSequence.codePoints()
Since Java 8 there's CharSequence.codePoints()
例如:
// if you want to work line by line, use Files.readAllLines()
// if you use Guava, there's also Guava's Files.toString() for reading the whole file into a String
byte[] bytes = Files.readAllBytes(Paths.get("test.txt"));
String text = new String(bytes, StandardCharsets.UTF_8);
IntStream codePoints = text.codePoints();
// do something with the code points
codePoints.forEach(codePoint -> System.out.println(codePoint));
这篇关于从 Java 输入流中读取下一个字符(完整的 Unicode 代码点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文