Java reads utf-8 encoded file, character by character

I have a file saved as utf-8 (actually saved by my application). How do you read it by character?

File file = new File(folder+name);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);

Two options are possible:

char c = dis.readByte()
char c = dis.readChar()
  • The first option works as long as you have saved only ascii characters, i.e. English.
  • The second option reads the first and second bytes of the file as one character.

The source file is written as follows:

File file = File.createTempFile("file", "txt");
FileWriter fstream = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fstream);
+3
source share
4 answers

You do not need a DataInputStream, which is for reading raw bytes. Use InputStreamReader , which allows you to specify the encoding of the input (UTF-8 in your case).

+7
source

, Java , / - . , , , .

http://java.sun.com/docs/books/tutorial/i18n/text/stream.html, , - .

Sun Java - .

+4

Reader (, BufferedReader)

Reader reader = BufferedReader ( FileReader ());

char c = reader.read();

+2

, , 128 (.. 8- 0), .

Java, , . , - , ...

edit : see dmazzoni answer.

-1
source

Source: https://habr.com/ru/post/1725570/


All Articles