java檔案讀取

來源:互聯網
上載者:User

標籤:輸入   異常   blog   log   except   input   tac   建立   數字   

  讀取可以是以位元組為單位讀取,可以是以字元為單位,也可以是一整行。

  使用FileInputStream就是按照位元組讀取的一次讀取一個位元組,常用於讀取二進位檔案片,聲音,影像等。

 1 try { 2             aFileInputStream=new FileInputStream(aFile); 3 //            FileInputStream aFileInputStream2=new FileInputStream("E:/a.txt"); 4             int ch; 5             while((ch=aFileInputStream.read())!=-1) 6             { 7                 System.out.print((char)ch); 8             } 9         } catch (FileNotFoundException e) {10             // TODO Auto-generated catch block11             e.printStackTrace();12         } catch (IOException e) {13             // TODO Auto-generated catch block14             e.printStackTrace();15         }finally {16             if(aFileInputStream!=null)17             {18                 try {19                     aFileInputStream.close();20                 } catch (IOException e) {21                     // TODO Auto-generated catch block22                     e.printStackTrace();23                 }            24             }25         }

  建議首先建立一個檔案引用,然後通過exists()檢查檔案是否存在,這樣會比注釋起來的那句代碼,直接讓它拋出異常處理要快,用完要注意把檔案輸入資料流關閉下面也是如此。

使用FileReader或者InputStreamReader,以字元為單位讀取一次讀取一個字元的位元組,常用於讀文本,數字類檔案。

 1 try { 2             FileReader fileReader=new FileReader(aFile); 3 //            FileReader aFileReader=new FileReader("E:/a.txt"); 4             int a; 5             while((a=fileReader.read())!=-1) 6             { 7                 System.out.print((char)a); 8             } 9 10             fileReader.close();11         } catch (FileNotFoundException e) {12             // TODO Auto-generated catch block13             e.printStackTrace();14         } catch (IOException e) {15             // TODO Auto-generated catch block16             e.printStackTrace();17         }

  但是需要注意對於windows下,\r\n這兩個字元在一起時,表示一個換行。但如果這兩個字元分開顯示時,會換兩次行,因此,要屏蔽掉\r,或者屏蔽\n。否則,將會多出很多空行。還可以直接讀取多個字元

 1 try { 2             FileReader fileReader=new FileReader(aFile); 3  4             char[] a=new char[20]; 5             while(fileReader.ready()) 6             { 7                 fileReader.read(a);//返回讀取長度 8             } 9             System.out.println(String.valueOf(a));10             fileReader.close();11         } catch (FileNotFoundException e) {12             // TODO Auto-generated catch block13             e.printStackTrace();14         } catch (IOException e) {15             // TODO Auto-generated catch block16             e.printStackTrace();17         }

這裡的關閉輸入資料流最好放在finally裡面,可以避免拋異常後沒有關閉的情況。

最後BufferedReader可以以行的形式讀取,常用於讀面向行的格式檔案。

 1 BufferedReader bufferedReader=null; 2         try { 3             bufferedReader=new BufferedReader(new FileReader(aFile)); 4             String aString; 5             while((aString=bufferedReader.readLine())!=null) 6             { 7                 System.out.println(aString);     8             } 9             10         } catch (FileNotFoundException e) {11             // TODO Auto-generated catch block12             e.printStackTrace();13         } catch (IOException e) {14             // TODO Auto-generated catch block15             e.printStackTrace();16         }finally {17             if (bufferedReader!=null) {18                 try {19                     bufferedReader.close();20                 } catch (IOException e) {21                     // TODO Auto-generated catch block22                     e.printStackTrace();23                 }24             }25         }

 

java檔案讀取

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.