本文主要給大家介紹java的InputStream 流的使用。
(1)FileInputstream: 子類,讀取數(shù)據(jù)的通道
使用步驟:
1.獲取目標(biāo)文件:new File()
2.建立通道:new FileInputString()
3.讀取數(shù)據(jù):read()
4.釋放資源:close()
1
2
3
4
|
//一些默認(rèn)要導(dǎo)入的包 import java.io.File; import java.io.FileInputStream; import java.io.IOException; |
1
2
3
4
5
6
7
8
9
10
11
|
public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //分別調(diào)用方法查看效果 test1(); System.out.println( "-------------------------------------------" ); test2(); System.out.println( "-------------------------------------------" ); test3(); System.out.println( "-------------------------------------------" ); test4(); } |
(2)讀取數(shù)據(jù)的三種方式
1.直接讀取 (一次只能一個字節(jié))
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)獲取目標(biāo)文件路徑 File file = new File( "C:\\Users\\joke\\Desktop\\Demo1.java" ); //(2)根據(jù)目標(biāo)文件路徑 建立通道: new FileInputStream(file) FileInputStream fileInputStream = new FileInputStream(file); //(3)讀取數(shù)據(jù) :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循環(huán)(效率低)
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
|
//方式二 循環(huán)遍歷 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循環(huán) 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[ ] 緩沖區(qū)(只能讀取指定的字節(jié)數(shù)不能讀取一個完整的文件)
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
|
//方式三 創(chuàng)建緩沖區(qū)(只能讀取制定的大小,不能讀取一個完整的文件) public static void test3() throws IOException{ File file = new File( "C:\\Users\\joke\\Desktop\\Demo1.java" ); FileInputStream fileInputStream = new FileInputStream(file); //創(chuàng)建緩沖區(qū),加快讀取數(shù)據(jù),確定要讀取的字節(jié)大小 byte [] bt = new byte [ 1024 ]; //read() 讀取字節(jié) int count = fileInputStream.read(bt); System.out.println(count); //顯示讀取到的字節(jié)數(shù) System.out.println( new String (bt, 0 ,count)); //將字節(jié)轉(zhuǎn)為字符串顯示 fileInputStream.close(); } |
4.緩沖區(qū)和循環(huán)結(jié)合。緩沖區(qū)一般設(shè)置為1024的倍數(shù)。理論上設(shè)置的緩沖區(qū)越大,讀取效率越高
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
|
//方式四 循環(huán)與緩沖區(qū)結(jié)合(效率高) 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); //緩沖區(qū)一般設(shè)置為1024的倍數(shù)。理論上設(shè)置的緩沖區(qū)越大,讀取效率越高 byte [] bt = new byte [ 1024 ]; int count = 0 ; //read返回 -1 時,證明已經(jīng)遍歷完 while ((count = fileInputStream.read(bt)) != - 1 ){ //字符串型顯示(從bt中的第0個字節(jié)開始遍歷count個長度) System.out.println( new String (bt, 0 ,count)); } fileInputStream.close(); long endTime = System.currentTimeMillis(); System.out.println( "讀取文件所花時間:" +(endTime-startTime)); } |
陌陌說:
在以上,對比第二個和第四個方法,會發(fā)現(xiàn)方法四的效率是比較高的,所以推薦使用的四個方法
在這里我們是直接拋出異常,除了拋出之外我們還可以使用
try{ }cater{ }finally{ }
的方式來處理異常
以上所述是小編給大家介紹的java IO流 之 輸入流 InputString()的使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/bigerf/archive/2016/12/06/6136557.html