- package cn.javastudy.p9.io.demo;
- import java.io.*;
- /*
- * 读取源文件
- * 把读取的源文件写到目标文件
- */
- public class CopyExpression {
- public static void main(String[] args) {
- copyData("d:\\demo.txt","E:\\demo.txt");
- }
- public static void copyData(String res, String des) {
- FileReader fr = null;
- FileWriter fw = null;
- char[] ch = new char[1024];
- int length = -1;
- try {
- fr = new FileReader(res);
- fw = new FileWriter(des);
- while ((length = fr.read(ch)) != -1)
- fw.write(ch, 0, length);
- fw.flush();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (fr != null)
- try {
- fr.close();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- if (fw != null)
- try {
- fw.close();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- }
- }
复制代码 复制文件的例子 |
|