Read next character (full unicode code point) from

2019-07-24 05:27发布

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).

What approach should I use?

标签: java utf-8
2条回答
我命由我不由天
2楼-- · 2019-07-24 05:57

Since Java 8 there's CharSequence.codePoints()

For example:

// 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));
查看更多
贪生不怕死
3楼-- · 2019-07-24 06:16

You can do this easily with an InputStreamReader by using the read() method. The read method will return an int which is a code point. Check out more here: http://docs.oracle.com/javase/tutorial/i18n/text/stream.html

FileInputStream fis = new FileInputStream("test.txt");
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
//Use isr.read() to read character by character.
查看更多
登录 后发表回答