Java serializes 96 FileInputStream method to parse and loop out all contents

Time:2020-10-31

1、 Analyze the available / skip / close / read (int [] a) method and use the read method to print out all the contents.

 

package com.bjpowernode.java_learning;

import java.io.*;

public class D96_1_CircleRead {

  public static void main(String[] args) throws Exception{

    FileInputStream fis = new FileInputStream("C:\Users\lenovo1\Workspaces\MyEclipse CI\Java_learning\src\com\bjpowernode\java_learning\D95_1_FileInputStreamAnalysis.java");

    byte[] bytes = new byte[1024];

    //1. Available method

    System.out.println(fis.available());

    System.out.println("===========");

    //2. Skip method

    fis.skip (2) ; // skip reading two bytes

    System.out.println("===========");   

    //3. Print the content circularly

    while(true) {

      int temp = fis.read(bytes);

      if(temp==-1) break;

      //Converts valid data in byte array to string

      System.out.print(new String(bytes,0,temp));

      //Note that here is the string interception. We need to know the return value of the read method here, so it is equivalent to creating an array to store each time

      //Read 1KB, then 1KB, 1KB output. The reason why I use 0 to temp is that I can fill the array when I read it, but when I read it, I can fill the array

      //At the last moment, a part of the array will be occupied. If we use fixed length 1024, we will print out the last part of the content

    }

   

    //4. Int read (byte [] b) reads data of up to b.length bytes from input stream into byte array

   

    System.out.println("===========");

    //As you can see, the available method is used to see how many bytes are left unread

    System.out.println(fis.available());

    //5. Remember to close the stream

    fis.close();

  }

}

​

 

2、 Source code:

D96_1_CircleRead.java

https://github.com/ruigege66/Java/blob/master/D96_1_CircleRead.java

2.CSDN:https://blog.csdn.net/weixin_44630050

3. Blog Garden: https://www.cnblogs.com/ruigege0000/

4. welcome to WeChat official account: Fourier transform, official account number, only for learning communication, background reply, “gift package”, get big data learning materials.