复制文件以及异常处理

代码实现特定 文件的复制粘贴。
在这里插入图片描述
如将D盘的大作业复制到F盘。
出现过拒绝访问的错误:你要读取文件,但路径是目录。

public class Demo1 {
    public static void main(String[] args) throws IOException {
        File srcPath = new File("D:\\大作业");
        File dstPath = new File("E:\\");
        copyDir(srcPath,dstPath);
    }

    private static void copyDir(File srcPath,File dstpath) throws IOException{
        if(srcPath.isDirectory()){
            //要复制的是一个文件夹
            File newFolder = new File(dstpath, srcPath.getName());//更新目录
            if(!newFolder.exists()) newFolder.mkdir();//创建目录
            File[] files = srcPath.listFiles();//得到目录下的文件
            for (File f:files//遍历文件
                 ) {
                copyDir(f,newFolder);
            }
        }else copy(srcPath,dstpath);

    }

    private static void copy(File srcfile, File dstPath) throws IOException {
     //字节缓冲流读写
        File dstFile = new File(dstPath,srcfile.getName());//目的文件
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcfile));//字节缓冲输入流
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream((new FileOutputStream(dstFile)));//字节缓冲输出流
        int len;
        byte[] b=new byte[1024];
        while((len=bufferedInputStream.read(b))!=-1){
            bufferedOutputStream.write(b,0,len);
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }
}

  1. try catch finally
    private static void copy(File srcfile, File dstPath)  {
        BufferedInputStream bufferedInputStream = null;//字节缓冲输入流
        File dstFile = new File(dstPath,srcfile.getName());//目的文件
        BufferedOutputStream bufferedOutputStream = null;//字节缓冲输出流
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream(srcfile));
            bufferedOutputStream = new BufferedOutputStream((new FileOutputStream(dstFile)));
            int len;
            byte[] b=new byte[1024];
            while((len=bufferedInputStream.read(b))!=-1){
                bufferedOutputStream.write(b,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bufferedInputStream!=null){
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bufferedOutputStream!=null){
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  1. JDK 7以后可以
try(定义流对象){
	...
}catch(...){
	...
}

自动释放资源,不用close
    private static void copy(File srcfile, File dstPath)  {
        File dstFile = new File(dstPath,srcfile.getName());//目的文件
        try(BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcfile));
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream((new FileOutputStream(dstFile)));){
            int len;
            byte[] b=new byte[1024];
            while((len=bufferedInputStream.read(b))!=-1){
                bufferedOutputStream.write(b,0,len);
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }
  1. JDK 9之后可以
定义流对象
定义流对象
try(流对象){
	
}catch(...){
	....
}
自动释放资源
private static void copy(File srcfile, File dstPath) throws IOException {
        File dstFile = new File(dstPath,srcfile.getName());//目的文件
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcfile));//字节缓冲输入流
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream((new FileOutputStream(dstFile)));//字节缓冲输出流
        try(bufferedInputStream;bufferedOutputStream){
            int len;
            byte[] b=new byte[1024];
            while((len=bufferedInputStream.read(b))!=-1){
                bufferedOutputStream.write(b,0,len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章