Thinking logic of computer programs (58) and thinking 58
In the previous section, we introduced how to process files in byte streams. we mentioned that for text files, byte streams do not have the encoding concept and cannot be processed by row, which is inconvenient to use, it is more suitable to use the streaming. This section describes the streaming.
First, we will briefly introduce the basic concepts of text files, the differences with binary files, encoding, and the differences between two streams and word throttling. Then we will introduce the main streams in Java, which include:
- Reader/Writer: The base class of the livestream. They are abstract classes.
- InputStreamReader/OutputStreamWriter: adapter class. The input is InputStream, the output is OutputStream, And the byte is converted to the bytes stream.
- FileReader/FileWriter: The Input Source and output target are the volume streams of files.
- CharArrayReader/CharArrayWriter: The Input Source and output target are character streams of the char array.
- StringReader/StringWriter: The Input Source and output target are String streams.
- BufferedReader/BufferedWriter: a decoration class that provides a buffer for input and output streams and supports reading and writing by row.
- PrintWriter: a decoration class that converts basic types and objects into the class output in string form.
In addition to these classes, Java also has a class category, which is similar to a Reader, but not a subclass of Reader. It can read basic types of strings, similar to the inverse operation of PrintWriter.
After understanding the byte stream and response stream, we will introduce the standard input output and error stream in Java.
Finally, we will summarize some simple and practical methods.
Basic Concepts
Text Files
As mentioned above, file processing requires binary thinking. From the binary perspective, we use a simple example to explain the differences between a text file and a binary file. For example, we want to store an integer of 123 and save it to the file test. dat in binary format. The code is:
DataOutputStream output = new DataOutputStream(new FileOutputStream("test.dat"));try{ output.writeInt(123);}finally{ output.close();}
Use UltraEdit to open the file. The following information is displayed:
{
Open the hexadecimal editor and display the following:
There are actually four bytes stored in the file. The decimal number of the Second-byte 7B is 123. That is to say, for the int type, the binary file is directly saved in the binary form of the int type. This binary form, if interpreted as a character, shows what character is related to the encoding, if encoded as a UTF-32BE, interpreted as a character, that is {.
If you use a text file to save an integer of 123, the code is:
OutputStream output = new FileOutputStream("test.txt");try{ String data = Integer.toString(123); output.write(data.getBytes("UTF-8"));}finally{ output.close();}
The Code converts integer 123 to a string, then prints its UTF-8 code to the file, and opens the file with UltraEdit, displaying what is expected:
123
Open the hexadecimal editor and display the following:
The file is actually stored in three bytes, 31 32 33 corresponds to the decimal number of 49 50 51, respectively, corresponding to the characters '1', '2 ', '3' ASCII encoding.
Encoding
In a text file, encoding is very important. The binary format corresponding to different encoding methods for the same character may be different. Let's take a look at the example for the same text:
Hello, 123, lauma
UTF-8 encoding, hexadecimal:
Each English or numeric character occupies one byte, and each Chinese Character occupies three bytes.
GB18030 encoding, hexadecimal:
English and numeric characters are the same as UTF-8 encoding, but Chinese characters are different. Each Chinese Character occupies two bytes.
UTF-16BE encoding, hexadecimal:
Each character occupies two bytes, whether it is an English or a Chinese character. UTF-16BE is also the encoding of characters in Java memory.
Ghost stream
Byte streams are read by byte, while byte streams are read by char. A char stores several bytes in the file, which is related to encoding. However, the byte stream encapsulates such details, the object we operate on is char.
It should be noted that a char is not exactly the same as a character. For the vast majority of characters, a character is a char, but we have previously introduced that for characters in the Supplementary Character Set, such'