编写应用程序,将程序文件的源代码复制到程序所在目录下的 temp.txt 文件中:
package com.hbsi;
import java.io.*;
public class TestReadToWrite {
public static void main(String[] args) throws IOException {
FileReader fr=null;
FileWriter fw=null;
try {
fr=new FileReader("src\\com\\hbsi\\TestReadToWrite.java");
fw=new FileWriter("temp.txt");
char[] buf=new char[1024];
int len=0;
while((len=fr.read(buf))!=-1){
fw.write(buf);
}
} catch (IOException e) {
System.out.println(e.toString());
}finally{
if(fw!=null)
try{
fw.close();
}catch (IOException e){
System.out.println(e.toString());
}
if(fr!=null)
try{
fr.close();
}catch (IOException e){
System.out.println(e.toString());
}
}
}
}