FileInputStream 讀取文件數據的輸入字節流


  1 package com.inputstream;
  2 
  3 /*
  4  File類: 用於描述一個文件或者文件夾的。
  5  
  6  通過File對象我們可以讀取文件或者文件夾的屬性數據,如果我們需要讀取文件的內容數據,那么我們需要使用IO流技術。
  7  
  8 IO流(Input Output)
  9 
 10 IO流解決問題: 解決設備與設備之間的數據傳輸問題。  內存--->硬盤   硬盤--->內存
 11 IO流技術:
 12 IO流分類:
 13     如果是按照數據的流向划分:
 14         輸入流
 15         輸出流
 16     如果按照處理的單位划分:
 17         字節流: 字節流讀取得都是文件中二進制數據,讀取到二進制數據不會經過任何的處理。
 18         字符流: 字符流讀取的數據是以字符為單位的 。 字符流也是讀取文件中的二進制數據,不過會把這些二進制數據轉換成我們能 識別的字符。  
 19                 字符流 = 字節流 + 解碼
 20 輸入字節流:
 21 --------| InputStream 所有輸入字節流的基類  抽象類
 22 ------------| FileInputStream  讀取文件數據的輸入字節流 
 23 使用FileInputStream讀取文件數據的步驟:
 24     1. 找到目標文件
 25     2. 建立數據的輸入通道。
 26     3. 讀取文件中的數據。
 27     4. 關閉 資源.
 28  */
 29 import java.io.File;
 30 import java.io.FileInputStream;
 31 import java.io.FileNotFoundException;
 32 import java.io.IOException;
 33 import java.io.InputStream;
 34 /**
 35  *   字節流
 36  * @author Administrator
 37  *
 38  */
 39 //方式2 : 使用循環讀取文件的數據
 40 /*class Inputtest{
 41 public static void inputRead1(String path){
 42         File file = new File("E://aa.txt");
 43         InputStream inputStream = null;
 44         try {
 45             inputStream = new FileInputStream(file);
 46             int str =0;
 47             while((str = inputStream.read())!=-1){
 48                 System.out.print((char)str);
 49             }
 50         } catch (FileNotFoundException e) {
 51             e.printStackTrace();
 52         }catch (IOException e){
 53             e.printStackTrace();
 54         }finally{
 55             if(inputStream != null){
 56             try {
 57                 inputStream.close();
 58             } catch (IOException e) {
 59                 e.printStackTrace();
 60             }
 61             }
 62         }
 63     }
 64 }
 65 
 66 public class Demo1 {
 67 
 68     public static void main(String[] args) {
 69         Inputtest inputtest = new Inputtest();
 70         inputtest.inputRead1("E://aa.txt");
 71     }
 72 }*/
 73 
 74 
 75 
 76 //方式3:使用緩沖 數組 讀取。    缺點: 無法讀取完整一個文件的數據。
 77 /*class inputTest{
 78     public static void inputRead2(){
 79         File file =new File("E://aa.txt");
 80         InputStream inputStream = null;
 81         try {
 82             inputStream = new FileInputStream(file);
 83             byte[] bs = new byte[1024];
 84             int length = inputStream.read(bs);
 85             String str = new String(bs,0,length);
 86             System.out.println("內容是:");
 87             System.out.println(str);
 88         } catch (FileNotFoundException e) {
 89             e.printStackTrace();
 90         }catch (IOException e){
 91             e.printStackTrace();
 92         }finally{
 93             if(inputStream != null){
 94             try {
 95                 inputStream.close();
 96             } catch (IOException e) {
 97                 e.printStackTrace();
 98             }
 99             }
100         }
101         }
102 }
103 public class Demo1 {
104 
105     public static void main(String[] args) {
106         inputTest inputTest = new inputTest();
107         inputTest.inputRead2();
108     }
109 }*/
110 
111 
112 
113 
114 //方式4:使用緩沖數組配合循環一起讀取。
115 class inputTest{
116     public static void inputRead3(){
117         //找到目標文件
118         File file = new File("E://aa.txt");
119         InputStream inputStream = null;
120         try {
121             //建立數據的輸入通道
122             inputStream = new FileInputStream(file);
123             //建立緩沖數組配合循環讀取文件的數據。
124             int length = 0;//保存每次讀取到的字節個數。
125             byte[] bs = new byte[1024];//存儲讀取到的數據    緩沖數組 的長度一般是1024的倍數,因為與計算機的處理單位。  理論上緩沖數組越大,效率越高
126             while((length = inputStream.read(bs))!=-1){// read方法如果讀取到了文件的末尾,那么會返回-1表示。
127                 String str = new String(bs,0,length);
128                 System.out.println("內容是"+"\n"+str);
129             }
130         } catch (FileNotFoundException e) {
131             e.printStackTrace();
132         } catch (IOException e){
133             e.printStackTrace();
134         }finally{
135             if(inputStream != null){
136                 try {
137                     inputStream.close();
138                 } catch (IOException e) {
139                     // TODO Auto-generated catch block
140                     e.printStackTrace();
141                 }
142             }
143         }
144         
145     }
146 }
147 
148 public class Demo1 {
149 
150     public static void main(String[] args) {
151         inputTest inputTest = new inputTest();
152         inputTest.inputRead3();
153     }
154 }
155 
156 
157 
158 
159 /*public class Demo1 {
160 
161     public static void main(String[] args) {
162         // TODO Auto-generated method stub
163 
164         //File file = new File("E://aa.txt");
165         Inputtest inputtest = new Inputtest();
166         File[] files = Inputtest.inputRead("E://aa.txt");
167         
168     }
169 
170 }
171 
172 
173 class Inputtest{
174     public static File[] inputRead(String path){
175         File file = new File("E://aa.txt");
176         File[] files = file.listFiles();
177         FileInputStream fileInputStream = null;
178         try {
179             fileInputStream = new FileInputStream(file);
180             int str=0;
181              
182              while(str != -1){
183              str = fileInputStream.read();
184             System.out.print((char)str);
185              }
186         } catch (FileNotFoundException e) {
187             e.printStackTrace();
188         }catch(IOException e){
189             e.printStackTrace();
190         }finally{
191             if(fileInputStream != null){
192             try {
193                 fileInputStream.close();
194             } catch (IOException e) {
195                 e.printStackTrace();
196             }
197             }
198         }
199         return files;
200     }
201 }*/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM