The file is divided into binary format and text format.
Data is represented in binary form in computers, in general, bytes are the smallest logical unit of data, so it can be said that the data is represented in the form of a sequence of bytes, whether in memory or in a disk file. If the data in memory is written directly into the file, the resulting file is binary format, if all the data is treated as a sequence of characters, re-encoding according to the selected character set, and then write the result of the encoding to a file, the resulting file is text format.
For example, there is an int type of data, the value is 1234, in memory in the form of D2, the result of storing to the file can be as follows:
In the case of writing to a text file, choosing a different encoding scheme will have different results. Use a diagram to simply represent the IO process for the following text file:
There is a recoding problem with the text file, such as recoding 00_00_04_d2 (int 1234) in memory as UTF-8 to 31_32_33_34. character data and byte sequences need to have a character set as a mapping relationship, the character set is like a dictionary, encoding according to the character of the corresponding encoding, as a stored byte sequence, decoding the time according to the byte sequence to find the corresponding character, can be read by the person. Different character sets have different mapping relationships, so the character set and encoding used when decoding are consistent.
Characters in memory, of course, exist in a sequence of bytes, using Unicode encoding, and you can use integet.tohexstring (int) to get its contents. So writing character data to a text file, there is a transition from one character encoding result to another character encoding result.
java.io File Categories