java 文件复制

Posted spp123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 文件复制相关的知识,希望对你有一定的参考价值。

public class CopyText { public static void main(String[] args) { copy_1(); } public static void copy_1() { FileWriter fw = null; FileReader fr = null; try { fw = new FileWriter("demo_desc.txt"); //与已有文件关联 fr = new FileReader("demo_src.txt"); int ch = 0; while ((ch = fr.read()) != -1) { fw.write(ch); } } catch (IOException e) { throw new RuntimeException("读写失败!"); } finally { if (fr != null) { try { fr.close(); } catch (IOException e) { throw new RuntimeException("关闭流失败!"); } } if (fw != null) { try { fr.close(); } catch (IOException e) { throw new RuntimeException("关闭流失败!"); } } } } public static void copy_2() { FileWriter fw = null; FileReader fr = null; try { fw = new FileWriter("demo_desc.txt"); //与已有文件关联 fr = new FileReader("demo_src.txt"); char[] buf = new char[1024]; int num = 0; while ((num = fr.read(buf)) != -1) { fw.write(buf, 0, num); } } catch (IOException e) { throw new RuntimeException("读写失败!"); } finally { if (fr != null) { try { fr.close(); } catch (IOException e) { throw new RuntimeException("关闭流失败!"); } } if (fw != null) { try { fr.close(); } catch (IOException e) { throw new RuntimeException("关闭流失败!"); } } } } }

以上是关于java 文件复制的主要内容,如果未能解决你的问题,请参考以下文章

用java控制流实现文件复制后,不能删除文件

jAVA基础 提高文件复制性能之多线程复制文件

jAVA基础 提高文件复制性能之多线程复制文件

Java 学习笔记 - 复制文件

JAVA题目求助,关于文件夹的复制

jAVA基础 提高文件复制性能之多线程复制文件