2019-05-30 Java Learning Diary day20 IO Stream

IO flow

Concept:

IO stream is used to process data transmission between devices

Java operates on data through streaming

Java classes for operation flow are in the IO package

Flow is divided into two kinds according to flow direction: input flow and output flow.

Flow can be divided into two types according to the type of flow operation:

Byte streams: Byte streams can manipulate any data because any data in a computer is stored in bytes

Character Stream: Character Stream can only operate pure character data, which is more convenient.

Commonly used parent classes for IO streams:

Abstract parent class of byte stream

    InputStream

    OutputStream

The abstract method of character class:

    Reader

    Writer

IO Programming

Import the classes in the IO package before use

IO exception handling when used

Release resources after use

 

InputStream

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

public class demo1_FileInputStraem {

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

    public static void demo1() throws FileNotFoundException, IOException {
        FileInputStream fis =new FileInputStream("xxx.txt");//Create stream objects
        int x=fis.read();  //Read a byte from the hard disk
        System.out.println(x);
        int y=fis.read();
        System.out.println(y);
        int z=fis.read();
        System.out.println(z);
        int c=fis.read();
        System.out.println(c);
        fis.close();   //Turn off and release resources
    }

}
case

 

The read () method reads a byte, and why does it return int instead of byte?

Because byte input streams can operate on any type of files, such as pictures, audio, etc., the underlying of these files are stored in the form up to now. If byte is returned every time it is read, 11111111 may be encountered in the middle of reading.

So 11111111 is type-1 of byte, our program will stop reading when it encounters-1, and the later data will not be read, so it is accepted with int type when reading, if 11111111 will be added in front of it.

Twenty-four zeros make up four bytes, so byte-1 becomes int-255, which ensures that the entire data is read, and the tagged-1 is int-type.

 

OutputStream

When creating objects, if there is no folder, it will help me create them.

If you have this file, you empty it first.

Parametric Addition true

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

public class demo2_OutputStream {

    public static void main(String[] args) throws IOException {
         FileOutputStream fos =new FileOutputStream("yyy.txt");
         //Create byte output stream objects, if not automatically create one
         
         fos.write(97);  //What the layman writes is a int Number, but to the file is a byte, will automatically remove the first three eight bits
         fos.write(98);
         fos.write(99);
         System.out.println(fos);
         fos.close();
         
         

    }
}
case

 

copy picture

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

public class demo3_Copy {

    public static void main(String[] args) throws IOException {
        //demo1();
        FileInputStream fil1=new FileInputStream("All of a sudden.mp3");  //Create input stream objects, associate binaries.jpg
        
        FileOutputStream fil2=new FileOutputStream("copy.mp3");//Create output stream objects, Associate copy.jpg
        
        int b;
        while ((b =fil1.read()) !=-1) {  //Read every byte continuously
            fil2.write(b);    //Write out each byte
            
        }
        fil1.close();//Turn off and release resources
        fil2.close();
    }

    public static void demo1() throws FileNotFoundException, IOException {
        FileInputStream fil1=new FileInputStream("1.JPG");  //Create input stream objects, associate binaries.jpg
        
        FileOutputStream fil2=new FileOutputStream("copy.jpg");//Create output stream objects, Associate copy.jpg
        
        int b;
        while ((b =fil1.read()) !=-1) {  //Read every byte continuously
            fil2.write(b);    //Write out each byte
            
        }
        fil1.close();//Turn off and release resources
        fil2.close();
    }

}
The first copy

 

Copy Video Graphics

Byte array copy-available () method

int read (byte [] b): read an array of bytes at a time

write (byte [] b): write an array of bytes at a time

available () Gets all the bytes of the read file

The second copy
public
static void main(String[] args) throws IOException { //demo1(); //demo2(); //May cause memory overflow FileInputStream fil1=new FileInputStream("All of a sudden.mp3"); FileOutputStream fil2=new FileOutputStream("copy.mp3"); //int len=fil1.available(); //System.out.println(len); byte [] arr=new byte[fil1.available()];//Create byte arrays the same size as files fil1.read(arr); //Read bytes from files into memory fil2.write(arr);//Write byte data from byte arrays to files fil1.close(); fil2.close(); } }

 

Define decimal arrays

  *write(byte [ ] b)

* write (byte [] b, int off, int len) writes out the number of bytes that are valid

public class demo_arrayCopy {

    public static void main(String[] args) throws IOException {
        //demo1();
        FileInputStream fis =new FileInputStream("xxx.txt");
        FileOutputStream fos =new FileOutputStream("aaa.txt");
        byte[] arr= new byte[2];
        int len;
        while ((len=fis.read(arr)) !=-1) {
            fos.write(arr,0,len);
            
        }
        fis.close();
        fos.close();
    }
}
The third copy

Keywords: PHP Java Programming

Added by vladj on Thu, 30 May 2019 21:03:00 +0300