This article mainly introduces the use of Java InputStream streams.
(1) FileInputstream: subclass, channel for reading data
Steps to use:
1. Get the target file: new File()
2. Create a channel: new FileInputString()
3. Read data: read()
4. Release resources: close()
//Some 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//Calling methods to view the effect test1();System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------(2) Three ways to read data
1. Read directly (only one byte at a time)
int date = fileInputStream.read(); char date3 = (char)fileInputStream.read();
//Print public static void test1() throws IOException{//(1) Get the target file path File file = new File("C://Users//joke//Desktop//Demo1.java");//(2) Create a channel based on the target file path: new FileInputStream(file)FileInputStream fileInputStream = new FileInputStream(file);//(3) Read data: read();int date = fileInputStream.read();//Here is int type int date2 = fileInputStream.read();//char date3 = (char)fileInputStream.read(); //Show System.out.println(date+"//"+date2+"//"+date3);//(4) Release the resource fileInputStream.close();}2. Use for loops alone (ine efficiency)
for(int i = 0; i < file.length();i++){ System.out.print((char)fileInputStream.read()); } //Method two-loop traversal public static void test2() throws IOException{//Test efficiency through time long startTime = System.currentTimeMillis();File file = new File("C://Users//joke//Desktop//Demo1.java");FileInputStream fileInputStream = new FileInputStream(file);//for loop for(int i = 0; i < file.length();i++){System.out.print((char)fileInputStream.read());}fileInputStream.close();long endTime = System.currentTimeMillis();System.out.println("Time taken to read 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));
//Method 3 create a buffer (can only read the specified size, not read a 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 to speed up reading of data, and determine the byte size to be read byte[] bt = new byte[1024];//read() read byte int count = fileInputStream.read(bt);System.out.println(count); //Show the number of bytes read System.out.println(new String (bt,0,count)); //Convert bytes to string to display fileInputStream.close();}4. Combining buffer and loop. The buffer is generally set to a multiple of 1024. The larger the buffer set, the higher the read efficiency.
byte[] bt = new byte[1024]; int count = 0; while((count = fileInputStream.read(bt)) != -1){ System.out.println(new String (bt,0,count)); } //Method four loops are combined with buffer (high efficiency) public static void test4() throws IOException{//Test efficiency by time long startTime = System.currentTimeMillis();File file = new File("C://Users//joke//Desktop//Demo1.java");FileInputStream fileInputStream = new FileInputStream(file);//The buffer is generally set to multiples of 1024. Theoretically, the larger the buffer set, the higher the reading efficiency byte[] bt = new byte[1024];int count = 0;//When read returns -1, it is proved that while((count = fileInputStream.read(bt)) != -1){//String type display (count lengths start from the 0th byte in bt) System.out.println(new String (bt,0,count));}fileInputStream.close(); long endTime = System.currentTimeMillis();System.out.println("Time taken to read the file:"+(endTime-startTime));}Momo said:
In the above, comparing the second and fourth methods, you will find that the efficiency of method 4 is relatively high, so the four recommended methods are used
Here we directly throw exceptions, in addition to throwing, we can also use
try{ }cater{ }finally{ }
To handle exceptions
The above is the use of InputString(), the input stream of java IO stream introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!