本文主要給大家介紹java的InputStream 流的使用。
(1)FileInputstream: 子類,讀取數據的通道
使用步驟:
1.獲取目標文件:new File()
2.建立通道:new FileInputString()
3.讀取數據:read()
4.釋放資源:close()
//一些默認要導入的包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//分別調用方法查看效果test1();System.out.println("-------------------------------------------");test2();System.out.println("-------------------------------------------");test3();System.out.println("-------------------------------------------");test4();}(2)讀取數據的三種方式
1.直接讀取 (一次只能一個字節)
int date = fileInputStream.read(); char date3 = (char)fileInputStream.read();
//方式一 直接打印public static void test1() throws IOException{//(1)獲取目標文件路徑File file = new File("C://Users//joke//Desktop//Demo1.java");//(2)根據目標文件路徑 建立通道: new FileInputStream(file)FileInputStream fileInputStream = new FileInputStream(file);//(3)讀取數據 :read();int date = fileInputStream.read();//這里是int類型int date2 = fileInputStream.read();//char date3 = (char)fileInputStream.read(); //以char類型顯示System.out.println(date+"http://"+date2+"http://"+date3);//(4)釋放資源fileInputStream.close();}2.單獨使用for循環(效率低)
for(int i = 0; i < file.length();i++){        System.out.print((char)fileInputStream.read());      }//方式二 循環遍歷public static void test2() throws IOException{//通過時間測試效率long startTime = System.currentTimeMillis();File file = new File("C://Users//joke//Desktop//Demo1.java");FileInputStream fileInputStream = new FileInputStream(file);//for循環for(int i = 0; i < file.length();i++){System.out.print((char)fileInputStream.read());}fileInputStream.close();long endTime = System.currentTimeMillis();System.out.println("讀取文件所花時間:"+(endTime-startTime));}3.Byte[ ] 緩沖區(只能讀取指定的字節數不能讀取一個完整的文件)
byte[] bt = new byte[1024]; int count = fileInputStream.read(bt); System.out.println(new String (bt,0,count));
//方式三 創建緩沖區(只能讀取制定的大小,不能讀取一個完整的文件)public static void test3() throws IOException{File file = new File("C://Users//joke//Desktop//Demo1.java");FileInputStream fileInputStream = new FileInputStream(file);//創建緩沖區,加快讀取數據,確定要讀取的字節大小byte[] bt = new byte[1024];//read() 讀取字節int count = fileInputStream.read(bt);System.out.println(count); //顯示讀取到的字節數System.out.println(new String (bt,0,count));//將字節轉為字符串顯示fileInputStream.close();}4.緩沖區和循環結合。緩沖區一般設置為1024的倍數。理論上設置的緩沖區越大,讀取效率越高
byte[] bt = new byte[1024];      int count = 0;      while((count = fileInputStream.read(bt)) != -1){        System.out.println(new String (bt,0,count));      }//方式四 循環與緩沖區結合(效率高)public static void test4() throws IOException{//通過時間測試效率long startTime = System.currentTimeMillis();File file = new File("C://Users//joke//Desktop//Demo1.java");FileInputStream fileInputStream = new FileInputStream(file);//緩沖區一般設置為1024的倍數。理論上設置的緩沖區越大,讀取效率越高byte[] bt = new byte[1024];int count = 0;//read返回 -1 時,證明已經遍歷完while((count = fileInputStream.read(bt)) != -1){//字符串型顯示(從bt中的第0個字節開始遍歷count個長度)System.out.println(new String (bt,0,count));}fileInputStream.close();long endTime = System.currentTimeMillis();System.out.println("讀取文件所花時間:"+(endTime-startTime));}陌陌說:
在以上,對比第二個和第四個方法,會發現方法四的效率是比較高的,所以推薦使用的四個方法
在這里我們是直接拋出異常,除了拋出之外我們還可以使用
    try{  }cater{  }finally{  }
的方式來處理異常
以上所述是小編給大家介紹的java IO流 之 輸入流 InputString()的使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答