java input, output (2)

1. Byte stream and
character stream The operation methods of byte stream and character stream are almost the same, the difference is only the data unit of operation is different. The data unit of byte stream operation is byte, and the data unit of character stream operation is character.
1. InputStream and Reader
InputStream and Reader are abstract base classes for all input streams, and their methods are available to all input streams.
The methods of InputStream and Reader are basically the same, except that InputStream reads bytes, and the parameters used are byte arrays (byte[]), while Reader reads characters, and the parameters used are char arrays (char[])
InputStream and Readers are abstract classes and cannot create instances by themselves, but they each have an input stream for reading files: FileInputStream and FileReader, which are both node streams and need to be directly associated with the specified file.
Second, OutputStream and Writer
OutputStream and Write are the base classes of all output streams, and their methods are very similar, and their methods are available to all output streams.
2. Print stream
In the entire IO package, print stream is a convenient class for outputting information, mainly including byte print stream (PrintStream) and character print stream (PrintWriter). The print stream provides a very convenient printing function, which can print any data type, such as: decimal, integer, string, etc.
The print stream has only output but no input. is a processing flow.
1. Format characters
Format characters are used to specify the data type and output format of the output item.
 d format: used to output decimal integers. There are the following usages:
%d: output according to the actual length of the integer data.
%md: m is the width of the specified output field. If the number of digits of the data is less than m, the left end will be filled with spaces; if it is greater than m, it will be output according to the actual number of digits.
%ld: Output long integer data.
 o format: output integer in unsigned octal format. For long integers, you can use the "%lo" format to output. It is also possible to specify the field width to output in "%mo" format.
 x format: output the integer in unsigned hexadecimal form. For long integers, it can be output in "%lx" format. You can also specify the field width to output in "%mx" format.
 c format: output one character.
 s format: used to output a string. There are several usages of
%s: for example: printf("%s", "CHINA") outputs the string "CHINA" (without double quotes).
%ms: The output string occupies m columns. If the length of the string itself is greater than m, it will break the limit of m and output all the strings. If the length of the string is less than m, it will be left-padded with spaces.
%-ms: If the length of the string is less than m, within the range of m columns, move the string to the left and fill with spaces to the right.
%m.ns: The output occupies m columns, but only the left-end n characters in the string are taken. The n characters are output on the right side of column m, with spaces on the left.
%-m.ns: The meanings of m and n are the same as above, and the n characters are output on the left side of the m column range, with spaces on the right. If n>m, the value of n is automatically taken, that is, the normal output of n characters is guaranteed.
 f format: used to output real numbers (including single and double precision) in decimal form. There are the following usages:
%f: Without specifying the width, the integer part is all output and 6 decimal places are output.
%m.nf: The output occupies a total of m columns, and there are n decimal places.
%-m.nf: The output occupies a total of n columns, in which there are n decimal places. If the width of the value is less than m, add a space at the right end.
3 conversion streams
The input/output stream system also provides two conversion streams, which are used to convert byte streams into character streams. InputStreamReader converts byte input streams into character input streams, and OutputStreamWriter converts byte output streams into character streams. character output stream.
4. To access the array
ByteArrayOutputStream and ByteArrayInputStream, when creating its instance, the program creates a buffer of byte array, and then uses the instance of ByteArrayOutputStream and ByteArrayInputStream to write or read byte data into the array. In network transmission, we often have to transmit many variables. We can use ByteArrayOutputStream to collect all variables together, and then send the data at one time.
ByteArrayOutputStream: It can capture the data of the memory buffer and convert it into a byte array. ByteArrayInputStream: It can convert byte arrays into input streams
CharArrayReader and CharArrayWrit are similar to ByteArrayOutputStream and ByteArrayInputStream.
5 access string
StringReader: used to convert the string into a character input stream. Then use the method provided by the character input stream to operate, and can also be provided to other advanced character input streams for use. For example, the character input stream can be provided to the BufferedReader input stream for use.
StringWriter: Cache all the read strings in memory, and then use the toString method to output all strings at once.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324033646&siteId=291194637