[205 days] Black Horse programmer 27 days video learning notes [Day20-up]

Two sentences of forced percussion

Faster ~Happy ~

20-01: Overview and classification of IO streams

concept

  1. IO streams are used to process data transfers between devices

  2. Java manipulates data through streaming

  3. Java classes for operation streams are in the IO package

  4. Flow is divided into input flow and output flow by flow direction.

  5. Streams are classified by operation type:

    1. Byte stream: A byte stream can manipulate any data because any data on the computer is stored in bytes.

    2. Character stream: Character stream can only operate on pure character data, which is more convenient.

Common parent of IO streams

  1. Abstract parent of byte stream

    1. InputStream

    2. OutputStream

  2. Abstract parent of character stream

    1. Reader

    2. Writer

  3. IO Program Writing

    1. Import classes in IO packages before use

    2. IO exception handling when in use [IO streaming handles data transfer between memory and hard disk, which may not have specified files]

    3. Release resources after use

20-(02-03): FileInputStream

  1. The end tag of the file is -1

  2. read() reads one byte at a time and returns the type int.

read file

package com.test.demo001;

import java.io.FileInputStream;
import java.io.IOException;

public class Demo009 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("yyy.txt");
        int a;
        while((a = fis.read())!=-1){
            System.out.println(a);   //Code table values read
        }
        fis.close();
    }
}

Why is the return value of the read() method int

Because the byte input stream can operate on any type of file, such as audio and video, the bottom of these files are stored in binary form. If byte is returned every time it is read, 11111111 may be encountered in the middle. This 11111 is -1 in the byte type. Because Java programs stop reading when they encounter -1, all subsequent data will be unreadable.When reading, it is received with the int type. If 11111111 prefaces it with 24 zeros or 4 bytes, then -1 of the byte type becomes 255 of the int type, which ensures that the entire data is read, while -1 of the end tag is the int type.[When a file is written with write, the first three 8 bits are also removed to ensure data integrity]

20-(04-05): FileOutputStream

  1. Although the write method writes out an int number, the first three eight bits of byte s are removed from the file.

package com.test.demo001;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo009 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("cccccc.txt");//Create an output stream object, or create one if no file is specified
        fos.write(97);  //Although an int is written out, the first three eight bits of byte are removed from the file
        fos.write(98);
        fos.write(99);
        fos.close();
    }
}
  1. When an output stream object is created, a file is created if it is not specified, and the contents of that file are emptied if there is one.

  2. If you want to continue without empty content, do the following:

FileOutputStream fos = new FileOutputStream("yyy.txt",true);

fos.write(97);
fos.write(98);

20-(06-10): Copy

Three methods of copying

  1. Byte-by-byte copy [inefficient, not recommended for development]

    package com.test.demo001;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Demo009 {
        public static void main(String[] args) throws IOException{
            FileInputStream fis = new FileInputStream("20.19_day20 summary.avi");
            FileOutputStream fos = new FileOutputStream("test.avi");
            int a;
            while((a = fis.read())!= -1){
                fos.write(a);
            }
            fis.close();
            fos.close();
            
        }
    }
    
  2. Packaged Copies [Easy memory overflow, not recommended for development]

        package com.test.demo001;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Demo009 {
        public static void main(String[] args) throws IOException{
            FileInputStream fis = new FileInputStream("20.19_day20 summary.avi");
            FileOutputStream fos = new FileOutputStream("test.avi");
    //        int a;
    //        while((a = fis.read())!= -1){
    //            fos.write(a);
    //        }
            
            byte[] arr = new byte[fis.available()];
            fis.read(arr);
            fos.write(arr);
            
            fis.close();
            fos.close();
            
        }
    }
  3. Define a copy of the decimal array [Development Recommendation]

General Form

package com.test.demo001;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo009 {
    public static void main(String[] args) throws IOException{
        FileInputStream fis = new FileInputStream("cccccc.txt");
        FileOutputStream fos = new FileOutputStream("bbbbbb.txt");
        byte[] arr = new byte[2];
        int len; //Number of valid characters read
        while((len = fis.read(arr))!= -1){
            fos.write(arr,0,len);
        }
        fis.close();
        fos.close();
    }
}

standard format

package com.test.demo001;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo009 {
    public static void main(String[] args) throws IOException{
        FileInputStream fis = new FileInputStream("cccccc.txt");
        FileOutputStream fos = new FileOutputStream("bbbbbb.txt");
        byte[] arr = new byte[1024 * 8];
        int len; //Number of valid characters read
        while((len = fis.read(arr))!= -1){
            fos.write(arr,0,len);
        }
        fis.close();
        fos.close();
    }
}

Copy schematic diagram

Keywords: Java

Added by mfos on Wed, 29 May 2019 20:05:56 +0300