Java——文件复制异常处理

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("写入流关闭失败");
            }
        }
    }
}

你可能感兴趣的:(java知识点,java,io流)