Java——文件复制异常处理

来源:互联网 发布:刀具查询软件 编辑:程序博客网 时间:2024/06/06 00:44
import java.io.*;class test{    public static void main(String[] args){        //复制文件        FileReader fr = null;        FileWriter fw = null;        try{            fr = new FileReader("temp.txt");            fw = new FileWriter("test_copy.txt");            char[] arr = new char[1024];            int num;            while((num = fr.read(arr))!=-1){                System.out.println(num);                fw.write(arr,0,num);            }        }catch(IOException e){            throw new RuntimeException("文件复制失败");        }        finally{            try{                if(fr!=null){fr.close();}            }catch(IOException e){                throw new RuntimeException("读取流关闭失败");            }            try{                if(fw!=null){fw.close();}            }catch(IOException e){                throw new RuntimeException("写入流关闭失败");            }        }    }}
原创粉丝点击