IO stream concept:

Let’s take a look at baidu Encyclopedia for IO flow explanation:

A stream is an abstract concept that represents the unstructured transfer of data. Inputs and outputs are streamed, and the data is treated as an unstructured byte order or sequence of characters. The operation of getting data from the flow is called an extract operation, and the operation of adding data to the flow is called an insert operation. The stream used for input and output operations is called an IO stream.

In other words, an IO stream is a stream of input and output

IO stream classification:

  1. According to the data flow: input stream and output stream

    Input streams can only be written and output streams can only be readCopy the code
  2. According to the data unit of the data operation: byte stream and character stream

    A byte stream is in bytes, a character stream is in characters a byte stream can handle most types of data (pictures, videos), a character stream can handle character types of dataCopy the code
  3. According to the different operation objects: node flow and processing flow

    From/to a specific I/o devices (such as disk, network) read/write data flow, flow is called node flow can be directly connected to the data source stream processing flow is the flow of an existing connection or encapsulation, by encapsulating flow to implement the data read/write function after processing flow is a node flow using the decorator pattern to add more featuresCopy the code

The four base classes for IO streams

  • InputStream

InputStream is the parent class of all input byte streams. It is an abstract class.

There are three main methods:

// Read a byte and return it as an integer (0~255). If -1 is returned, the end of the input stream is reached. int read(); // Reads a sequence of bytes and stores it in an array buffer, returning the actual number of bytes read, or -1 if read before the end of the input stream. int read(byte[] buffer); // Read the length of bytes and store them in a byte array buffer, starting at off and up to len. Return the actual number of bytes read, or -1 if read before and at the end of the input stream. int read(byte[] buffer, int off, int len);Copy the code
  • OutputStream

OutputStream is the parent class of all output byte streams. It is an abstract class.

It mainly includes the following four methods:

// Writes a byte of data to the output stream, which is the lower 8 bits of parameter B. void write(int b) ; // Write an array of bytes to the output stream. void write(byte[] b); // Writes len bytes from an array of type bytes starting at the specified position (off) to the output stream. void write(byte[] b, int off, int len); // Write all the data cached in the output stream to the destination. void flush();Copy the code
  • Reader

Reader is the parent class of all input character streams and is an abstract class.

There are three main methods:

// Reads a character and returns it as an integer (0~255), if -1 is at the end of the input stream. Int the read (); // Reads a series of characters and stores them in an array buffer, returning the actual number of characters read, or -1 if it was read before the end of the input stream. Int read (char [] cbuf); // Read the length of characters and store them in an array buffer, starting at off and up to len. Return the actual number of characters read, or -1 if read before the end of the input stream. int read(char[] cbuf, int off, int len)Copy the code
  • Writer

Writer is the parent class of all output character streams. It is an abstract class.

It mainly includes the following six methods:

// Write a character to the output stream. The byte data is the lower 16 bits of parameter B. void write(int c); Void write(char[] cbuf) void write(char[] cbuf) void write(char[] cbuf) void write(char[] cbuf, int offset, int length); // Writes the characters from a string to the output stream. void write(String string); // Writes the length of a string starting with offset to the output stream. void write(String string, int offset, int length); // Write all the data cached in the output stream to the destination. void flush()Copy the code

Writer has two more methods than OutputStream. Writer supports writing characters and string data

Node flow (file flow)

FileReader is a subclass of Reader and also has three methods for read

  1. useint read()methods
@Test public void FileReaderTest() throws IOException{ //1. File File = new File(" File path "); FileReader fr=new FileReader(file); //2. Int data=fr.read(); while (data! =-1){ System.out.print((char)data); data=fr.read(); } //4. Close the stream fr.close(); }Copy the code
  1. useint read(char [] cbuf)methods
//read(char[] cbuf): return the number of characters to be read in cbuf array char[] cbuf =new char[5]; int len; while((len=fr.read(cbuf))! Cbuf. length for(int I =0; i<len; i++){ System.out.print(cbuf[i]); }}Copy the code

If I <cbuf.length = I <cbuf.length

Fr reads the contents of the array and prints out the contents of the array each time we iterate.

When the file content is HelloWorld123, the output data is HelloWorld123ld

The reason is that the ld is not overwritten by the last read

  1. useint read (char[] cbuf,int off,int len)methods
        int read (cbuf,0,len)
Copy the code

Use FileReader and FileWriter to copy files

@Test public void FileReaderFileWriteTest(){ FileReader fr = null; FileWriter fw = null; Try {//1. Create File object File srcFile = new File(" source File address "); File destFile = new File(" destination "); Fr = new FileReader(srcFile); //2. fw = new FileWriter(destFile); //3. Char [] cbuf = new char[5]; int len; While (len=fr.read(cbuf))! =-1){ fw.write(cbuf,0,len); } } catch (IOException e) { e.printStackTrace(); } finally {//4. Close resource try {if(fr! =null){ fr.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(fw! =null){ fw.close(); } } catch (IOException e) { e.printStackTrace(); }}}Copy the code

FileInputStream and FileOutputStream are generally used to process non-text files. The rest of the steps are the same as above

Buffer flow

Function: Improves stream reading and writing speed

Use a buffer stream for replication

public void BufferedStreamTest(){ BufferedInputStream bis = null; BufferedOutputStream bos = null; Try {//1. File srcfile=new File(""); File destfile=new File(""); //2. FileInputStream fis = new FileInputStream(srcfile); FileOutputStream fos = new FileOutputStream(destfile); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); Byte [] buffer = new byte[10]; //3. int len; while((len=bis.read(buffer))! =-1){ bos.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } finally {//4. If (bos! =null){ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if(bis! =null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); }}}}Copy the code

Transformation flows

Function: provides conversion between byte streams and character streams

InputStreamReader: Converts a byte input stream into a character input stream, inherited from Reader

OutputStreamWriter: Converts a character output stream into a byte output stream, which is inherited from Writer

The data stored in the computer are binary Numbers, the text message we see on the computer screen is to convert binary, that will be displayed after exist between encoding and decoding process, the mutual transformation must follow some rules, namely the encoding and decoding all follow the same rules to literal information, according to the normal if the encoding and decoding using different rules, There will be garbled code situation.

  • Encoding: character, string –> byte
  • Decode: byte -> character, string

The InputStreamReader constructor

  • InputStreamReader(InputStream in) : creates a default character set character InputStream
  • InputStreamReader(InputStream in, String charsetName) : Creates a character stream of the specified character set

OutputStreamWriter constructor

  • OutputStreamWriter(OutputStream in): Creates a character stream that uses the default character set.
  • OutputStreamWriter(OutputStream in, String charsetName): Creates a character stream of the specified character set.

The conversion stream references the blog

Object flow

ObjectInputStream ObjectOutputStream

Objects in Java can be written to or restored from the data source

Serialization: a mechanism for holding basic data types or objects with the ObjectOutputStream class

Deserialization: A mechanism for reading base data types or objects using the ObjectInputStream class

When passing and saving objects. To ensure the integrity and transitibility of the object: ensure that the object is serialized

Implement serialization:

  1. Object implements the Serializable interface
  2. Writes the object to a file
  3. The file is read out and converted into an object

Question:

  1. Static properties cannot be serialized
  2. The Transient property is not serialized
  3. Serialized version number serialVersionUID

The serialVersionUID generated by the JDK tool is a number generated based on the object’s property information, which means that whenever an object’s property changes a little, its serialized version number changes synchronously

Serialized reference blog