Java reads UTF-8 encoded files character by character

I have a file saved as UTF - 8 (actually saved by my application) How do you read it word for word?

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

The two options seem to be:

char c = dis.readByte()
char c = dis.readChar()

>As long as you only store ASCII characters, the first option is English. > The second option reads the first and second bytes of the file as a character

The original document was prepared as follows:

File file = File.createTempFile("file","txt");
FileWriter fstream = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fstream);

Solution

You do not need a datainputstream to read raw bytes Use inputstreamreader, which allows you to specify the encoding of the input (UTF - 8 in your case)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>