Java IO programming — character stream and byte stream

In Java The file class in the IO package is the only program processing class related to the file itself, but the file can only operate the file itself and not the content of the file. In other words, in actual development, the core meaning of IO operation is input and output operation. For the program, the input and output may come from different environments. For example, when browsing through a computer connected to the server, the client actually sends a message, and then the server responds after receiving this message.

For the server or client, what is actually transmitted is a form of data stream processing, and the so-called data stream refers to byte data. The processing form of this stream is @ H_ 301_ 22@java.io There are two types of support in the package:

·Byte processing stream: OutputStream (output byte stream), InputStream (input byte stream);

·Character processing stream: writer (output character stream), reader (input character stream);

All stream operations should be carried out in the following unified steps, taking the file processing process as an example:

·If you want to read and write files now, you must find a file path through the file class;

·Instantiate the parent object through the subclass of byte stream or character stream;

·The input and output operations of data are realized by using the methods in byte stream or character stream;

·Stream operation belongs to resource operation, and resource operation must be closed;

1. Byte output stream: OutputStream

Byte data is mainly implemented in byte type. OutputStream class can be used to output byte content. The basic definition of this class is as follows:

Firstly, it can be found that this class implements two interfaces, so the basic correspondence is as follows:

The OutputStream class defines a public output operation standard, in which three content output methods are defined.

However, a core problem that needs to be noted is that the OutputStream class is an abstract class after all. If this abstract class wants to obtain an instantiated object, it should be completed by turning up the subclass instance according to the traditional understanding. If the file processing operation is to be performed now, the fileoutputstream subclass can be used:

Because the processing relationship needs to be transformed upward in the end, the core focus of the fileoutputstream subclass at this time can be placed on the construction method:

·[overwrite] construction method: public fileoutputstream (file file) throws FileNotFoundException;

·[append] construction method: public fileoutputstream (file, Boolean append) throws FileNotFoundException;

Example: use the OutputStream class to output content

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
分享
二维码
< <上一篇
下一篇>>