utf-8 - Java uses FileWriter to write int type data to a file, the file data is wrong?
phpcn_u1582
phpcn_u1582 2017-05-17 09:58:09
0
1
599

(I know my title is very vague, because I really don’t know how to describe it)

The cause of the matter is that an Android APP was developed in Java. One of the functions is to use FileWriter to write int data to a data file. This data file is created using File's createNewFile(String filename) method, and the data written to the file are all int types. (FileWriter’s write(int) method)

For the convenience of subsequent processing, each data segment in the data file is divided by 4 (int)-1 (written in front of each data segment). Logically speaking, the first 16 bytes of this file should be ff. But the actual data file is like this:

I see familiar problems, such as UTF8 encoding errors, but I don’t know very well why there are encoding errors if int is written using write, and it is not writing String to the file.

So I would like to ask everyone, what should I do in this situation (pure int data file)? Is there any other way to read the data?

Thanks in advance!

phpcn_u1582
phpcn_u1582

reply all(1)
仅有的幸福

FileWrite's Write(int) method cannot write pure int files because the int here is actually a char character. When your int data cannot find the corresponding char value in the unicode code table, garbled characters will appear. You can provide a toString method to convert it into a string
If you want to write real int data

       

     File file = new  File("b.txt");
FileWriter fw = new FileWriter(file);
fw.write((Integer.toString(87)));
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template