IO stream: byte stream, buffer stream, data stream

1. What is flow? ---IO stream

1. The role is to transfer data from one location to another.
         (1): Streams are classified according to the transmission direction:
         input stream input: external data transmission (in, read) to the current program,
         output stream ouput: data transmission (write out) of the current program to the outside world.
           (2) Classify
                         byte stream according to read and write units: the minimum unit for reading and writing is a byte
                         character stream: the minimum unit for reading and writing is one character
          (3) Classification according to whether it is directly connected to external devices:
                         node stream: directly connected to the outside world The flow type processing flow connected to the device
                         : indirect, generally the node flow is encapsulated internally.
          (4) Classified according to the efficiency:
                         low-level stream ,
                         high-level stream
        2.IO stream:
          File, RandomAccessFile are all in the java.io package.
                 A series of types of IO streams are also under this package. 3: Parent class   InputStream: is the parent class (abstract type) of the byte input stream   OutputStream: is the parent class (abstract type) of the byte output stream   Reader: is the parent class (abstract type) of the character input stream
 




  Writer: is the parent class of character output stream (abstract type)
4: File input and output stream: (file byte stream)
  1) FileOutputStream
   constructor:
   FileOutputStream(File file)
   FileOutputStream(String filepath)
Note:
the constructor of the output stream When there are two parameters, and the second parameter is
  boolean append
  false, it means that no appending is performed, and the write operation is performed from the head of the file.
   When true, it means appending and writing out from the end of the file.
   1) FileOutputStream(File file,boolean append)
   2)FileOutputStream(String filepath,boolean append)
  2) FileInputStream
     constructor:
    FileInputStream(File file)
     FileInputStream(String filepath)        
                 3) **Common method:    
              1)write(int num) : Write the lower eight bits of num binary
                2)write(byte[] bytes)
               3)write(byte[] bytes,int index,int length)       
               4)int read(): read a byte and put it into the lower eight bits of the int type
               5)read(byte[] bytes): read valid bytes, stored in a byte array.
       6)int length = fis.read(bytes);
7)String str = new String(bytes,"utf-8");
               4) Similar: Random read and write file types also operate on bytes;

 2. Byte buffer stream

1. Function:
In order to improve the writing efficiency and reduce the number of interactions, a buffer is designed.
The data is first filled into the buffer. If the data in the buffer is full, it will be
         emptied at one time.
2. Important constructors:
1) BufferedInputStream(fis)
2) BufferedOutpuStream(fos)
  This buffered stream takes the file byte stream as a parameter.
  FileOutputStream fos = new FileOutputStream("b.txt", true/false);
        3. Common methods:
flush(): The function is to clear the data in the buffer.
        4. Features:
Before the stream is closed, the data in the buffer will be emptied.
5. When the two streams, FileOutputStream and BufferedOutputStream are involved, when
closing the stream, you only need to close the buffer stream bos.close();
because: 
the outermost stream is closed, and the inner stream is also automatically closed
 

3. Data input and output stream

1. Stream: It also takes the byte stream as a parameter.
     DataInputStream
    DataOutputStream 
2. Constructor:
     DataInputStream(InputStream in) 
     DataOutputStream(OutputStream out)  
3 For example:
DataOutputStream dos =
  new DataOutputStream(
  new FileOutputStream("c.txt",true));
  dos.writeInt(21); Table: Append
  dos .writeUTF("Hello!!");
dos.close();
DataInputStream dis = 
new DataInputStream(
new FileInputStream("c.txt"));
int num = dis.readInt(); String str = dis.readUTF( ); 4. Common methods Write : dos.writeInt(21); dos.writeFloat(3.5F);  




 
  dos.writeUTF("Hello!!"); read: int num = dis.readInt(); float num1 = dis.readFloat(); String str = dis.readUTF();




dis.close();




Guess you like

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