FileInputStream file byte input stream

1. Concept

Based on the memory, the data in the disk file is read into the memory in byte form.

2. Constructor

public FileInputStream(File file)

public FileInputStream(String pathname)

Both of these create a byte input stream pipeline and connect it to the source file.

Three. Methods

public int read(): Returns one byte each time it is read. If no data is found to be read, -1 is returned.

public int read(byte[] buffer): Each time a byte array is used to read data, the number of bytes read in the byte array is returned. If no data is found to be read, -1 is returned.

4. Execution

Method 1: Read byte by byte

1. Code
package org.example;

import java.io.*;

public class day05 {
    public static void main(String[] args) throws IOException {
        //1. Create a file byte input stream pipeline and connect it to the source file: both methods are OK
        InputStream f1 = new FileInputStream(new File("D:\temp\day05\a.txt"));
        InputStream f2 = new FileInputStream("D:\temp\day05\a.txt");
        //2. Read the byte data of the file
        int b1 = f1.read();
        System.out.println(b1);
        System.out.println((char) b1);
        int b2 = f1.read();
        System.out.println(b2);
        System.out.println((char) b2);
        int b3 = f1.read();
        System.out.println(b3);
    }
}
2. Results

The above code is too troublesome to read byte by byte, and reading Chinese characters will be garbled. Let’s optimize it below.

Method 2: Loop reading

1. Code
package org.example;

import java.io.*;

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

        InputStream f1 = new FileInputStream("D:\temp\day05\b.txt");
        int b; //Used to remember the bytes read
        while((b = f1.read()) != -1){
            System.out.print((char)b);
        }
        f1.close();
    }
}

The reading performance of the above code is very poor, and reading Chinese characters will be garbled, which needs further improvement; the stream must be closed after use to release system resources.

2. Results

Method 3: Read multiple bytes at a time

1. Code
package org.example;

import java.io.*;

public class day05 {
    public static void main(String[] args) throws IOException {
        //b.txt content: abcdefg
        InputStream f1 = new FileInputStream("D:\temp\day05\b.txt");
        //Start reading the byte data in the file, reading multiple bytes each time
         byte[] buffer = new byte[4];
         int len = f1.read(buffer);
         String s = new String(buffer);
        System.out.println(s);
        System.out.println("Number of bytes read" + len);
        int len2 = f1.read(buffer);
        String s2 = new String(buffer);
        System.out.println(s2);
        System.out.println("Number of bytes read" + len2);
        f1.close();
    }
}
2. Results

Under normal circumstances, the result of the second read should be efg instead of efgd

3.Improvement
package org.example;

import java.io.*;

public class day05 {
    public static void main(String[] args) throws IOException {
        //b.txt content: abcdefg
        InputStream f1 = new FileInputStream("D:\temp\day05\b.txt");
        //Start reading the byte data in the file, reading multiple bytes each time
         byte[] buffer = new byte[4];
         int len = f1.read(buffer);
         String s = new String(buffer);
        System.out.println(s);
        System.out.println("Number of bytes read" + len);
        int len2 = f1.read(buffer);
        String s2 = new String(buffer,0,len2);
        System.out.println(s2);
        System.out.println("Number of bytes read" + len2);
        f1.close();
    }
}
4. Results

This code needs to be optimized, use loops to further optimize

Method 4: Loop reading

1. Code
package org.example;

import java.io.*;

public class day05 {
    public static void main(String[] args) throws IOException {
        //b.txt content: abcdefg
        InputStream f1 = new FileInputStream("D:\temp\day05\b.txt");
        //Start reading the byte data in the file, reading multiple bytes each time
        byte[] buffer = new byte[4];
        int len;
        while ((len = f1.read(buffer)) != -1) {
            String s = new String(buffer, 0, len);
            System.out.print(s);
        }
        f1.close();
    }
}
2. Results

5. Questions

The reading performance of the above code has been improved, but garbled characters will still be generated when reading Chinese characters.

Solution 1: Define a byte array as large as the file and read all bytes of the file at once (not recommended)

Method 1

1. Code
package org.example;

import java.io.*;

public class day05 {
    public static void main(String[] args) throws IOException {
        //c.txt content: We are together abcd
        InputStream f1 = new FileInputStream("D:\temp\day05\\c.txt");
        //The 19 here can be obtained using f1.length()
        byte[] buffer = new byte[19];
        int len;
        while ((len = f1.read(buffer)) != -1) {
            String s = new String(buffer, 0, len);
            System.out.print(s);
        }
        f1.close();
    }
}
2. Results

Method 2

1. Code
package org.example;

import java.io.*;

public class day05 {
    public static void main(String[] args) throws IOException {
        //c.txt content: We are together abcd
        InputStream f1 = new FileInputStream("D:\temp\day05\\c.txt");
        final byte[] bytes = f1.readAllBytes();
        System.out.println(new String(bytes));
    }
}
2. Results

The above code needs to be optimized. If the file is particularly large, an exception will be thrown when using readAllBytes().

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 138432 people are learning the system