- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.lang.RuntimeException;
- class CopyTest
- {
- public static void main(String[] args)throws IOException
- {
- method_2();
-
- }
- public static void method_1()throws IOException
- {
- FileReader fr=new FileReader("MathDemo.java");
- FileWriter fw=new FileWriter("D:\\CopyTest1.java");
- int num=0;
- while((num=fr.read())!=-1){
- fw.write((char)num);
- }
- fr.close();
- fw.close();
- }
- public static void method_2(){
- FileReader fr=null;
- FileWriter fw=null;
- try{
- fr=new FileReader("MathDemo.java");
- fw=new FileWriter("D:\\CopyTest2.java");
- char[] buf=new char[1024];
- int len=0;
- while((len=fr.read(buf))!=-1){
- fw.write(buf,0,len);
- }
- }
- catch(IOException e){
- throw new RuntimeException("复制文件失败");
- }
- finally{
- try{
- if(fr!=null)
- fr.close();
- }
- catch(Exception ex){
-
- }
- try{
- if(fw!=null)
- fw.close();
- }
- catch(Exception ex){
-
- }
- }
- }
- }
复制代码 |
|