/**
需求:将本目录下IODemo.java的内容拷贝到IODemo_Copy.java中
*/
- import java.io.*;
- class CopyDemo
- {
- public static void main(String[] args)
- {
- copy("IODemo.java","IODemo_Copy.java");
- }
- public static void copy(String out,String in)
- {
- FileWriter fw = null;
- FileReader fr = null;
- try
- {
- fw = new FileWriter(in);
- fr = new FileReader(out);
- //创建读写对象
- char[] ch = new char[1024];
- int i = 0;
- while ((i = fr.read(ch)) != -1)
- //将数据读入内存
- {
- fw.write(ch,0,i);
- //将内存中数据写入目标文件
- }
- }
- catch (IOException e)
- {
- e.toString();
- }
- finally
- {
- try
- {
- if(fw !=null)
- fw.close();
- }
- catch (IOException e)
- {
- e.toString();
- }
- try
- {
- if(fr != null)
- fr.close();
- }
- catch (IOException e)
- {
- e.toString();
- }
- //不同的流对象需要分别try
- }
- }
- }
复制代码
|
|