java IO操作:FileInputStream,FileOutputStream,FileReader,FileWriter執行個體

來源:互聯網
上載者:User

標籤:類對象   char   tar   ring   font   資料   style   cep   類執行個體化   

FileInputStream

 
 
  1. <span style="font-family:Verdana;">import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.InputStream;  
  4.   
  5. public class TestFileInputStream {  
  6.     public static void main(String[] args) throws Exception { // 異常拋出, 不處理  
  7.         // 第1步: 使用File類找到一個檔案  
  8.         File f = new File("c:" + File.separator + "test.txt");// 聲明File 對象  
  9.         // 第2步: 通過子類執行個體化父類對象  
  10.         InputStream input = null;  
  11.         // 準備好一個輸入的對象, 通過對象多態性進行執行個體化  
  12.         input = new FileInputStream(f);  
  13.         // 第3步:進行讀操作, 所有的內容讀到此數組中  
  14.         byte b[] = new byte[1024];  
  15.         int len = input.read(b);  
  16.         // 第4步:關閉輸入資料流  
  17.         input.close();  
  18.         // 把byte數組變為字串輸出  
  19.         System.out.println("讀入資料的長度:" + len);  
  20.         System.out.println("內容為:" + new String(b, 0, len));  
  21.     }  
  22. }</span>  

FileOutputStream

 
 
  1. <span style="font-family:Verdana;">import java.io.File;  
  2. import java.io.FileOutputStream;  
  3. import java.io.OutputStream;  
  4.   
  5. public class TestFileOutputStream {  
  6.     public static void main(String[] args) throws Exception { // 異常拋出,不處理  
  7.         // 第1步: 使用File類找到一個檔案  
  8.         File f = new File("c:" + File.separator + "test.txt"); // 聲明File對象  
  9.         // 第2步: 通過子類執行個體化父類對象  
  10.         OutputStream out = null;  
  11.         // 準備好一個輸出的對象, 通過對象多態性,進行執行個體化  
  12.         out = new FileOutputStream(f);  
  13.         // 第3步: 進行寫操作, 準備一個字串  
  14.         String str = "Hello World!!!";  
  15.         // 只能輸出byte數組,所以將字串變為byte數組  
  16.         byte b[] = str.getBytes();  
  17.         // 將內容輸出,儲存檔案  
  18.         out.write(b);  
  19.         // 第4步:關閉輸出資料流  
  20.         out.close();  
  21.     }  
  22. }</span>  

FileReader

 
 
  1. <span style="font-family:Verdana;">import java.io.File;  
  2. import java.io.FileReader;  
  3. import java.io.Reader;  
  4.   
  5. public class TestFileReader {  
  6.     public static void main(String[] args) throws Exception { // 異常拋出, 不處理  
  7.         // 第1步:使用File類找到一個檔案, 聲明File對象  
  8.         File f = new File("d:" + File.separator + "test.txt");  
  9.         // 第2步:通過子類執行個體化父類對象  
  10.         Reader reader = null;  
  11.         // 準備好一個輸入的對象, 通過對象多態性進行執行個體化  
  12.         reader = new FileReader(f);  
  13.         // 第3步:進行讀操作, 所有的內容讀到此數組中  
  14.         char c[] = new char[1024];  
  15.         int len = reader.read(c);  
  16.         // 第4步:關閉輸入資料流  
  17.         reader.close();  
  18.         // 把char數組變為字串輸出  
  19.         System.out.println("內容為:" + new String(c, 0, len));  
  20.     }  
  21. }</span>  

FileWriter

 
 
    1. <span style="font-family:Verdana;">import java.io.File;  
    2. import java.io.FileWriter;  
    3. import java.io.Writer;  
    4.   
    5. public class TestFileWriter {  
    6.     public static void main(String[] args) throws Exception { // 異常拋出, 不處理  
    7.         // 第1步:使用File類找到一個檔案, 聲明File對象  
    8.         File f = new File("c:" + File.separator + "test.txt");  
    9.         // 第2步:通過子類執行個體化父類對象  
    10.         Writer out = null;  
    11.         // 準備好一個輸出的對象, 通過對象多態性, 進行執行個體化  
    12.         out = new FileWriter(f);  
    13.         // 第3步:進行寫操作, 準備一個字串  
    14.         String str = "Hello World!!!";  
    15.         out.write(str);  
    16.         out.flush();  
    17.         // 第4步:關閉輸出資料流  
    18.         out.close();  
    19.     }  
    20. }</span>  

java IO操作:FileInputStream,FileOutputStream,FileReader,FileWriter執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.