JAVA IO learns to organize byte stream

Abstract base classes: InputStream , OutputStream.

Byte streams can operate on any data.

Note: The array used by the character stream is a character array. char[] chs

          The array used by the byte stream is an array of bytes. byte[] bt

FileOutputStream  fos  = new FileOutputStream("a.txt");

fos.write("abcde");//Write the data directly to the destination.

fos.close();//Only close the resource.

FileInputStream fis = new FileInputStream("a.txt");

//fis.available();//Get the number of bytes of the associated file.

 

//If the file size is not very large.

// can do this.

 

byte[] buf = new byte[fis.available()];//Create a just-right buffer.

//But this has a drawback, that is, when the file is too large and the size exceeds the content space of the jvm, the memory will overflow.

fis.read(buf);

System.out.println(new String(buf));

 

Requirements: copy a picture.

BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("1.jpg"));

BufferedOutputStream  bufos = new BufferedOutputStream(new FileOutputStream("2.jpg"));

int by = 0;

while((by = bufis.read()) != -1)  {

        bufos.write(by);

}

bufos.close();

bufis.close();

Stream objects currently learned:

character stream:

        FileReader

        FileWriter

        BufferedReader

        BufferedWriter

byte stream:

        FileInputStream

        FileOutputStream

        BufferedInputStream

        BufferedOutputStream

The read() method of a byte stream reads a byte. Why not return byte type, but int type?

Because the read method returns -1 when it reaches the end.

In the data being manipulated, it is easy to have multiple 1s in a row, and reading 8 1s in a row is -1.

Causes reads to stop prematurely.

 

So the read byte is upgraded to an int type value, but only the original byte is kept, and the remaining binary bits are filled with 0.

The specific operation is: byte&255 or byte&0xff

 

For the write method, one byte can be written at a time, but an int type value is received.

Only the lowest byte (8 bits) of the value of type int is written.

 

Simply put: the read method improves the read data. write converts the data of the operation.

 

 

Transform stream:

Features:

1: It is a bridge between byte stream and character stream.

2: The stream object can perform encoding conversion of the specified encoding table on the read byte data.

 

When to use it?

1: When there is a conversion action between bytes and characters.

2: When the data of the stream operation needs to be specified in the encoding table.

 

Specific objects reflect:

1: InputStreamReader: A byte-to-character bridge.

2: OutputStreamWriter: A character-to-byte bridge.

 

These two stream objects are members of the character stream hierarchy.

 

Then they have the role of conversion, and themselves are character streams. So when constructing, you need to pass in the byte stream object.

 

Constructor:

InputStreamReader(InputStream): Initialized through this constructor, using the default encoding table GBK of the system

InputStreamReader(InputStream,String charSet): Initialized by this constructor, the encoding table can be specified.

 

OutputStreamWriter(OutputStream): Initialized by this constructor, using the default encoding table GBK of the system.

OutputStreamWriter(OutputStream,String charSet): Initialized by this constructor, the encoding table can be specified.

 

A subclass of conversion stream when manipulating a file's character stream object.

Reader

        --->InputStreamReader

                        --->FileReader

Writer

        --->OutputStreamWriter

                        --->FileWriter

 

Convert the read method in the stream. has been integrated into the coding table,

When the read method of the byte stream is called at the bottom layer, one or more bytes of data obtained are temporarily stored, and the specified encoding table is checked. If the encoding table is not specified, the default encoding table is checked. Then the read method of the conversion stream can return a character such as Chinese.

 

The conversion stream has completed the action of encoding conversion. For the FileReader of the directly manipulated text file, there is no need to redefine it. As long as the conversion stream is inherited and its method is obtained, the character data in the text file can be directly manipulated.

 

 

Notice:

When using FileReader to manipulate text data, this object uses the default encoding table. If you want to use the specified encoding table, you must use the conversion stream.

 

FileReader fr = new FileReader("a.txt");//Operate the data in a.txt using the default encoding GBK of this system. The data in the operation a.txt is also the default code GBK of this system.

InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));

The meaning of the code in these two sentences is the same.

 

If the character data in the file in a.txt is encoded in the form of utf-8.

Then when reading, you must specify the encoding table.

Then the transform stream must be used.

InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"utf-8");

 

Basic laws of stream operations.

    1: Identify data sources and data sinks (data purpose).

            In fact, it is to clarify the input stream or the output stream.

    2: Make it clear whether the data being manipulated is plain text data.

            In fact, it is to clarify the character stream or the byte stream.

 

Data source: keyboard System.int, stream object at the beginning of hard disk File, memory (array).

Data sink: console System.out, stream object at the beginning of hard disk File, memory (array).

 

need:

1: Save the data entered by the keyboard to a file.

        Data source: System.in

 

        Since it is the source, the input stream is used, and the available systems are InputStream, Reader.

        Because of the plain text data entered by the keyboard for a certain period of time, a Reader that operates exclusively on character data can be used.

        Byte read stream when the stream corresponding to System.in is found. So to convert it, convert bytes to characters.

        So use the Reader system: InputStreamReader

        Next, is there a need to improve efficiency? If necessary, then add a buffer to the character stream: BufferedReader

        BufferedReader bur = new BufferedReader(new InputStreamReader(System.in));

 

 

        Data sink: a file, hard disk.

        Since the time data sink, then a certain time output stream, you can use OutputStream, Writer.

        All text data is stored in the file, so it is more convenient to use a character stream: Writer.

 

        Because the operation is a file. So use FileWriter from Writer.

        Whether to improve efficiency? Yes, then use BufferedWriter.

        BufferedWriter bufr = new BufferedWriter(new FileWriter("a.txt"));

 

    Additional requirements: I want to store these text data in a file according to the specified encoding table.

        Since it is text data and is still written to a file, the system used is still Writer.

        Because you want to specify the encoding table, you need to use the conversion stream in Writer, OutputStreamWriter.

        Whether to improve efficiency, yes, choose BufferedWriter.

        Note: Although it is ultimately a file, FileWriter cannot be selected. Because the object is using the default encoding table.

        The output conversion stream needs to receive a byte output stream, so use the OutputStream system, and finally output to a file,

        Then it is necessary to use the byte stream object of the file that can be operated in the OutputStream system: FileOutputStream.

        //String charSet = System.getProperty("file.encoding");

        String charSet = "utf-8";

        BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("a.txt"),charSet);

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325942984&siteId=291194637