Detailed Knowledge Points about Streaming in Java

  • 2021-06-28 09:17:22
  • OfStack

What is a stream?

Stream: A pipe that connects programs and devices for data transmission. Stream is a pipe.

Classification of streams:

Four Basic Abstract streams (input stream, output stream, character stream, byte stream) File Stream Buffer Stream Conversion Flow Data stream 1 must be a class, but class 1 must not be a stream print Stream object Stream

It can be divided into input stream and output stream according to the direction of data flow.

It can be divided into byte stream and character stream according to the unit of processing data. (One character is two bytes)

It can be divided into node (original) flow and processing (package) flow by function.

Byte Stream Character Stream
Input stream InputStream Reader
Output stream OutputStream Writer


File stream:

Reads the contents of a file and outputs it to the display, counting the number of bytes read.When strings represent the file paths of the operating system, we can use \\ and / as path delimiters for folders.

What is a byte stream?

FileInputStream FileOutputStream

What is a character stream?

FileReader FileWriter


eg:
import java.awt.*;
public class test{
   public static void main(String[] args){
    FileReader fr=new FileRead("D:\\share\\test.java"); 
    FileWriter fw=new FileWrite("d:/zhangsan.haha");
   int ch;
       ch=fr.read();
 while(-1 != ch){
     fw.write(ch); // take test File fr Read data from the program and write data from the program to the program fw Of zhangsan In folder 
     ch=fr.read();
}
  fw.flush();
  fr.close();
  fw.close();
}
}

The difference between a character stream and a byte stream:

Byte stream can complete assignment of all format files

Character stream values can copy text files, but cannot copy video format files.

Because bytes do not require decoding and encoding, there is a problem with decoding and encoding bytes into characters.

Byte streams can read data from devices in all formats, but character streams can only read and write data from devices in text format.If the content of the text file is output to the display through a byte stream, the output of Chinese characters will be scrambled.

Buffer Stream: Buffered

Buffer streams are input and output streams with buffers

Buffer streams can significantly reduce our access to IO and protect our hard drives.

Buffer flow is itself a processing flow and must be dependent on node flow, which is the flow wrapped around the original node and is equivalent to a pipeline included in the pipeline.

BufferedInputStream: Buffered output stream that allows multiple bytes of data to be written to the hard disk once

BufferedOutputStream: Buffered input stream that allows multiple bytes of data to be read into the program at once

BufferedWriter BufferedReader can improve the speed of reading and writing text file contents


eg:
import java.awt.*;// Buffered byte streams process files faster than non-buffered byte streams. 
public class test{
   public static void main(String[] args){
   BufferedInputStream bis=new BufferedInputStream (new FileRead("D:\\share\\test.java")); 
    BufferedOutputStream bos= new BufferedOutputStream (new FileWrite("d:/zhangsan.haha"));
byte[] buf=new byte[1024];
   int len;
     len=bis.read(buf);
 while(-1 != len){
     bos.write(buf,0,len );
     len=bis.read(buf);
}
  bos.flush();
  bos.close();
  bis.close();
}
}

Conversion stream:

An outputStreamWrite stream is a stream that converts an OutputStrean stream into an Writer stream

An inputStreamReader stream is a stream that converts an inputStrean stream to an Reader stream

print stream:

print stream has only output, no input

PrintWriter Output Character PrintStream Output Bytes

Differences between printWriter and printStream:

printWriter provides all printing methods for PrintStream, which can encapsulate both OutputStream and Writer. PrintStream can only encapsulate byte streams of type OutputStream.

Redirection of standard input and output:

The programming implementation inputs the data entered by the keyboard into the A file, and outputs the error information to the B file if the input is incorrect.


eg:
public class test{
    public static void main(String[] args){
   PrintStream psOut=null;
   PrintStream psError=null;
  Scanner sc=null;
     try{
     psOut=new PrintStream("d:/Out.txt");
     psError=new PrintStream("d:/error.txt");
  sc=new Scanner(System.in);
   int num;
   System.setOut(psOut);
  System.setErr(psError);
  while(true){
   num=sc.nextInt();
  System.out.println(num);
  }
}  
catch(Exception e){
  System.out.println(" The error message is: ");
  e.printStackTrace();
}
}
}
}

summary


Related articles: