標籤:數組 match dex int class ack nbsp str 字串
寫入文字檔
1. 關聯讀入的檔案,使用Reader 和 FileReader
2. 關聯寫出的檔案,使用Writer和 FileWriter
3. 建立緩衝 char數組,用於接收讀取到的文本資訊
4. 將文本讀入到 緩衝數組(buff)中
5. 輸出讀取到的文本資訊
6. 寫出讀取到的檔案
7. 關閉寫出檔案流
8. 關閉讀取檔案流
package com.machuang.io.charIO;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.Reader;import java.io.Writer;public class textWrite { public static void main(String[] args) { // 與檔案建立聯絡 Reader reader = null; Writer writer = null; try { reader = new FileReader("F:/win10/test/a.txt"); writer = new FileWriter("F:/win10/test/aCopy.txt", true); // 建立 char 字串緩衝數組 char[] cbuf = new char[1024]; // 讀取和寫入 int len = 0; while(-1 != (len = reader.read(cbuf))) { writer.write(cbuf); writer.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if(null != writer) { writer.close(); } if(null != reader) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } } } // match main function}
java--文字檔寫入