學習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;
1 public static void main(String[] args) throws IOException {
2 // TODO Auto-generated method stub
3
4 //分別調用方法查看效果
5 test1();
6 System.out.println("-------------------------------------------");
7 test2();
8 System.out.println("-------------------------------------------");
9 test3();
10 System.out.println("-------------------------------------------");
11 test4();
12 }
(2)讀取數據的三種方式
1.直接讀取 (一次只能一個字節)
int date = fileInputStream.read();
char date3 = (char)fileInputStream.read();
1 //方式一 直接打印
2 public static void test1() throws IOException{
3
4 //(1)獲取目標文件路徑
5 File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
6
7 //(2)根據目標文件路徑 建立通道: new FileInputStream(file)
8 FileInputStream fileInputStream = new FileInputStream(file);
9
10 //(3)讀取數據 :read();
11 int date = fileInputStream.read();//這裡是int類型
12 int date2 = fileInputStream.read();//
13 char date3 = (char)fileInputStream.read(); //以char類型顯示
14 System.out.println(date+"\\"+date2+"\\"+date3);
15
16 //(4)釋放資源
17 fileInputStream.close();
18 }
2.單獨使用for循環(效率低)
for(int i = 0; i < file.length();i++){
System.out.print((char)fileInputStream.read());
}
1 //方式二 循環遍歷
2 public static void test2() throws IOException{
3
4 //通過時間測試效率
5 long startTime = System.currentTimeMillis();
6
7 File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
8 FileInputStream fileInputStream = new FileInputStream(file);
9
10 //for循環
11 for(int i = 0; i < file.length();i++){
12 System.out.print((char)fileInputStream.read());
13 }
14
15 fileInputStream.close();
16
17 long endTime = System.currentTimeMillis();
18 System.out.println("讀取文件所花時間:"+(endTime-startTime));
19 }
3.Byte[ ] 緩沖區(只能讀取指定的字節數不能讀取一個完整的文件)
byte[] bt = new byte[1024];
int count = fileInputStream.read(bt);
System.out.println(new String (bt,0,count));
1 //方式三 創建緩沖區(只能讀取制定的大小,不能讀取一個完整的文件)
2 public static void test3() throws IOException{
3
4
5 File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
6
7 FileInputStream fileInputStream = new FileInputStream(file);
8
9 //創建緩沖區,加快讀取數據,確定要讀取的字節大小
10 byte[] bt = new byte[1024];
11
12 //read() 讀取字節
13 int count = fileInputStream.read(bt);
14 System.out.println(count); //顯示讀取到的字節數
15 System.out.println(new String (bt,0,count));//將字節轉為字符串顯示
16
17 fileInputStream.close();
18 }
4.緩沖區和循環結合。緩沖區一般設置為1024的倍數。理論上設置的緩沖區越大,讀取效率越高
byte[] bt = new byte[1024];
int count = 0;
while((count = fileInputStream.read(bt)) != -1){
System.out.println(new String (bt,0,count));
}
1 //方式四 循環與緩沖區結合(效率高)
2 public static void test4() throws IOException{
3
4 //通過時間測試效率
5 long startTime = System.currentTimeMillis();
6
7 File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
8
9 FileInputStream fileInputStream = new FileInputStream(file);
10
11 //緩沖區一般設置為1024的倍數。理論上設置的緩沖區越大,讀取效率越高
12 byte[] bt = new byte[1024];
13
14 int count = 0;
15 //read返回 -1 時,證明已經遍歷完
16 while((count = fileInputStream.read(bt)) != -1){
17 //字符串型顯示(從bt中的第0個字節開始遍歷count個長度)
18 System.out.println(new String (bt,0,count));
19 }
20
21 fileInputStream.close();
22
23 long endTime = System.currentTimeMillis();
24 System.out.println("讀取文件所花時間:"+(endTime-startTime));
25 }
陌陌說:
在以上,對比第二個和第四個方法,會發現方法四的效率是比較高的,所以推薦使用的四個方法
在這裡我們是直接拋出異常,除了拋出之外我們還可以使用
try{ }cater{ }finally{ }
的方式來處理異常