本文主要給大家介紹java的InputStream 流的使用。
(1)FileInputstream: 子類,讀取數據的通道
使用步驟:
1.獲取目標文件:new File()
2.建立通道:new FileInputString()
3.讀取數據:read()
4.釋放資源:close()
1
2
3
4
|
1
2
3
4
5
6
7
8
9
10
11
|
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.直接讀取 (一次只能一個字節)
1
2
|
int date = fileInputStream.read(); char date3 = ( char )fileInputStream.read(); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//方式一 直接打印 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+ "\\" +date2+ "\\" +date3); //(4)釋放資源 fileInputStream.close(); } |
2.單獨使用for循環(效率低)
1
2
3
|
for ( int i = 0 ; i < file.length();i++){ System.out.print(( char )fileInputStream.read()); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//方式二 循環遍歷 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[ ] 緩沖區(只能讀取指定的字節數不能讀取一個完整的文件)
1
2
3
|
byte [] bt = new byte [ 1024 ]; int count = fileInputStream.read(bt); System.out.println( new String (bt, 0 ,count)); |
1
2
3
4
5
6
7
8
9
10
11
12
|
//方式三 創建緩沖區(只能讀取制定的大小,不能讀取一個完整的文件) 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的倍數。理論上設置的緩沖區越大,讀取效率越高
1
2
3
4
5
|
byte [] bt = new byte [ 1024 ]; int count = 0 ; while ((count = fileInputStream.read(bt)) != - 1 ){ System.out.println( new String (bt, 0 ,count)); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//方式四 循環與緩沖區結合(效率高) 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()的使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/bigerf/archive/2016/12/06/6136557.html