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("释放资源失败"); } } } } }
字符流复制文本文件 字符数组 缓冲
最新推荐文章于 2021-02-20 15:09:04 发布