1. 程式人生 > >字元流複製文字檔案 字元陣列 緩衝

字元流複製文字檔案 字元陣列 緩衝

package cn.niit.demo10;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
字元流複製文字檔案
字元陣列 緩衝 */
public class Copy1 {
    public static void main(String[] args) {
        FileReader fr = null;
FileWriter fw = null;
        try {
            fr = new FileReader("c:\\1.txt"
); fw = new FileWriter("d:\\1.txt"); //定義字元陣列 char[] chars = new char[1024]; int len = 0; while ((fr.read(chars)) != -1) { fw.write(chars, 0, len); fw.flush(); } } catch (IOException e) { System.out.println(e); throw new RuntimeException("
檔案複製失敗!"); } finally { try { if (fw != null) { fw.close(); } } catch (IOException e) { throw new RuntimeException("釋放資源失敗"); } finally { try { if (fr != null) { fr.close();
} } catch (IOException e) { throw new RuntimeException("釋放資源失敗"); } } } } }