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