[review] IO flow in java

Keywords: Java

Basic concept of IO flow:

  • IO flow is used to process data transmission between devices (between memory and hard disk, equivalent to the input and output flow establishing a pipeline between them, the pipeline establishment must occupy resources, so we need to close the flow after using the input and output flow)
Java operates on data by means of flow, which can be classified into two types by operation type:
  • Byte Stream: byte Stream can operate any data type (text, picture, audio, video, etc.), and any data in the computer is stored in the form of bytes (general class ends with Stream)
  • Character stream: character stream can only operate on pure character data (general classes end with Reader and Writer)
    Common parent class of IO flow:
The architecture of the flow is as follows
  • Closeable < closeable > (close())
  • Flushing < refreshable > (flush())

inputStream / OutputStream / Reader / Writer

All byte input streams are subclasses of inputStream
All byte output streams are subclasses of OutputStream
All character input streams are subclasses of readers
All character output streams are subclasses of Writer
All the above four streams implement the closeable interface
The above two output streams all implement the flushable interface

FileInputStream and FileOuputStream

       @Test
    public void Test1() throws IOException {

        //Create an instance of FileInputStream and open the file at the same time
        FileInputStream fis = new FileInputStream("E:\\project\\jichuyufa\\src\\day23\\IODemo\\ioTest");

        //Read the contents of the specified file
        byte[] b = new byte[3];
        int len = fis.read(b);

        while (len != -1){
            //System.out.println(new String((b,0,len));
            len=fis.read(b);
        }
        //Closed flow
        fis.close();

    }


	@Test
    public void test2(){
        String str = "askdjfkajdskfj";

        FileOutputStream fos = null;
        try{
            //1. Create an instance of FileOutputStream and open the specified file at the same time
            fos = new FileOutputStream("./hello1.txt");

            //2. Write the specified content to the target location
            fos.write(str.getBytes());
        	}catch(IOException e){
            e.printStackTrace();
        	}finally{
            if(fos != null){
                //3. closing flow
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

InputStream and OutputStream

public class FileCopy {

    public static void main(String[] args) throws IOException {
        //demo1();
        //demo2();
        
        //Mode 3: define the small array to read the file, and write the data in the small array to the file (the standard file copy method in development)
        FileInputStream fis=new FileInputStream("a.txt");
        FileOutputStream fos=new FileOutputStream("c.txt");
        
        byte[] bytes=new byte[1024*2];
        int len;
        //Here, if the byte array is not read, len gets the machine code of the corresponding data every time, and the file size written by the copy is the total number of bytes of the machine code
        while((len=fis.read(bytes))!=-1){
            fos.write(bytes, 0, len);
        }
        
        fis.close();
        fos.close();
        
    }

    private static void demo2() throws FileNotFoundException, IOException {
        //Mode 2: read the whole file together in the copy (available())
        //In this way, if a large file is read, when the whole large file is read, it will cause memory overflow
                FileInputStream fis=new FileInputStream("a.txt");
                FileOutputStream fos=new FileOutputStream("c.txt");
                
                int available = fis.available();//Get file size
                //Create a byte array of the same size as the file
                byte[] b= new byte[available];
                //Read byte array into memory
                fis.read(b);
                //Write the array of byte array to the file to be copied
                fos.write(b);
                
                fis.close();
                fos.close();
    }

    private static void demo1() throws FileNotFoundException, IOException {
        //Mode 1: read the copy byte by byte (this method is also the copy core code of IO stream)
        FileInputStream fis=new FileInputStream("a.txt");
        FileOutputStream fos=new FileOutputStream("c.txt");
        
        int a;
        while((a=fis.read())!=-1){
            fos.write(a);
        }
        
        fis.close();
        fos.close();
    }

}

Use IO stream to write the contents of one txt text to multiple txt texts

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class IODemo {
public static void main(String[] args) {

    try {
        InputStream is = new FileInputStream(new File("E:/eula.1028.txt"));
        OutputStream os1 = new FileOutputStream(new File("G:/demo1.txt"));
        OutputStream os2 = new FileOutputStream(new File("G:/demo2.txt"));
        byte[] buffer = new byte[1024];
        int len = 0;
        int L = 0;
        while ((len = is.read(buffer)) != -1){
            if (L < 1024){
                os1.write(buffer, 0, len);
                L += len;
            } else {
                os2.write(buffer, 0, len);
                L += len;
            }

        }

    is.close();
    os1.close();
    os2.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}
Published 26 original articles, won praise 1, visited 884
Private letter follow

Posted by ScubaDvr2 on Sat, 14 Mar 2020 22:38:13 -0700