The use of java IO stream input stream InputString of

  • 2020-05-19 04:50:53
  • OfStack

This article focuses on the use of java's InputStream stream.

(1) FileInputstream: subclass, channel for reading data

Use steps:

1. Get the target file: new File ()

2. Establish channels: new FileInputString ()

3. Read data: read ()

4. Release resources: close ()


//1 The packages to be imported by default 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// Call the method separately to see the effect 
test1();
System.out.println("-------------------------------------------");
test2();
System.out.println("-------------------------------------------");
test3();
System.out.println("-------------------------------------------");
test4();
}

(2) three ways to read data

1. Direct reading (only 1 byte at a time)


int date = fileInputStream.read();
      char date3 = (char)fileInputStream.read();

// way 1  Print directly 
public static void test1() throws IOException{
//(1) Gets the destination file path 
File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
//(2) According to the destination file path   Build channels:  new FileInputStream(file)
FileInputStream fileInputStream = new FileInputStream(file);
//(3) Read the data   : read();
int date = fileInputStream.read();// Here is the int type 
int date2 = fileInputStream.read();//
char date3 = (char)fileInputStream.read(); // In order to char Types of display 
System.out.println(date+"\\"+date2+"\\"+date3);
//(4) Release resources 
fileInputStream.close();
}

2. for loops alone (inefficient)


for(int i = 0; i < file.length();i++){
        System.out.print((char)fileInputStream.read());
      }

// way 2  To iterate over 
public static void test2() throws IOException{
// Efficiency is measured by time 
long startTime = System.currentTimeMillis();
File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
FileInputStream fileInputStream = new FileInputStream(file);
//for cycle 
for(int i = 0; i < file.length();i++){
System.out.print((char)fileInputStream.read());
}
fileInputStream.close();
long endTime = System.currentTimeMillis();
System.out.println(" Time spent reading the file: "+(endTime-startTime));
}

3.Byte[] buffer (can only read the specified number of bytes and cannot read a complete file)


byte[] bt = new byte[1024];
      int count = fileInputStream.read(bt);
      System.out.println(new String (bt,0,count));

// way 3  Create buffer (can only read the specified size, cannot read 1 Complete file) 
public static void test3() throws IOException{
File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
FileInputStream fileInputStream = new FileInputStream(file);
// Create a buffer, speed up the reading, and determine the size of the bytes to read 
byte[] bt = new byte[1024];
//read()  Read the bytes 
int count = fileInputStream.read(bt);
System.out.println(count); // Displays the number of bytes read 
System.out.println(new String (bt,0,count));// Converts the byte to a string display 
fileInputStream.close();
}

4. Combination of buffer and loop. Buffer 1 is normally set to a multiple of 1024. Theoretically, the larger the buffer, the more efficient the read


byte[] bt = new byte[1024];
      int count = 0;
      while((count = fileInputStream.read(bt)) != -1){
        System.out.println(new String (bt,0,count));
      }

// way 4  Combining loops with buffers (efficient) 
public static void test4() throws IOException{
// Efficiency is measured by time 
long startTime = System.currentTimeMillis();
File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
FileInputStream fileInputStream = new FileInputStream(file);
// The buffer 1 A set to 1024 Multiples. Theoretically, the larger the buffer, the more efficient the read 
byte[] bt = new byte[1024];
int count = 0;
//read return  -1  When, the proof has been traversed 
while((count = fileInputStream.read(bt)) != -1){
// String display (from bt The first of 0 The byte traversal begins count A length) 
System.out.println(new String (bt,0,count));
}
fileInputStream.close();
long endTime = System.currentTimeMillis();
System.out.println(" Time spent reading the file: "+(endTime-startTime));
}

Momo said.

Above, comparing the second method and the fourth method, we can find that method 4 is relatively efficient, so the four methods are recommended to be used

Here we are throwing an exception directly, which we can use in addition to throwing

 try{  }cater{  }finally{  }

To handle exceptions


Related articles: