Write of FileInputStream class and read of fileoutstream class

Time:2021-12-24

Byte stream

preface: have you ever thought about the videos we usually watch,
In what form are audio and pictures stored What form are they stored?


Byte streamThere are two kinds. One isoutputOne isinputHere are allOutput input streamThe top-level parent of.


Byte output streamOutputStream
Byte input streamInputStream


Fileoutputstream class(Output class)

Subclass of output stream FileOutputStreamClass is a file output stream

closeMethod: close the resources released by this stream. Don’t waste memory.
writeMethods (there are three uses) let me show you:


package com.kjzz.pojo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class day1 {
    public static void main(String[] args) throws IOException {
        // a 
        File file = new File("A.txt");
        FileOutputStream fos = new FileOutputStream(file);
        //The first is to write out data
        fos.write(97); // a 
        fos.write(98); // b
        //Close resource
        fos.close();

        // b
        FileOutputStream fo= new FileOutputStream("B.txt");
        //Second byte array
        Byte [] B = "can you think no" getBytes();
        fo. write(b); //  Can you think no
        //Close resource
        fo.close();

        // c
        //Create a stream object with a file name
        FileOutputStream f = new FileOutputStream("C.txt");
        byte [] c = "abQAQ".getBytes();
        //The third option is to select three numbers after two subscripts in the byte array di2
        f.write(c,2,3); // QAQ
        //Close resource
        f.close();
        
    }
}

summary:1. Remember to use when the stream operation is completedClose methodFree system memory. 2. The third usage of writeThe second is the subscriptThe third is choicebehindA few digits.


Is fileoutstream dataTracing + Line feed operationHow do you use it?

1. Whether to append is to add false (default) to fileoutstream.

2. Line feed operation in Windows system\r\nIt’s a line break

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

public class day2 {
    public static void main(String[] args) throws IOException {

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

        byte [] b = "abc".getBytes();
        
        fos.write(b);
        fos.write("\r\n".getBytes());
       
        //Close resource
        fos.close();
        
    }
}
Output results:
ABC (pay attention to line feed)
ABC (secondary operation)
ABC (three runs)

FileInputStream class (input class)

Is a file input stream that reads bytes from the file.

His method:

read :Byte array read data

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

public class day3 {
    public static void main(String[] args) {
        try {
            //Create stream object with file name
            FileInputStream fis= new FileInputStream("BB.txt");
            //Define variable as a valid unit 0
            int len;
            //Select unit 
            byte [] b = new byte [2];
            
            while ( (len = fis.read(b)) != -1 ) {
                System.out.println(new String(b,0,len));
            }
            
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
Output results:
ab
cd
e

summary: select a constant of 0 and use the while loop.

InputStream output stream (read) OutputStream input stream (write).