码迷,mamicode.com
首页 > 其他好文 > 详细

复制文件的异常处理

时间:2019-08-29 15:58:44      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:stream   trace   fileinput   cto   red   image   else   directory   list   

技术图片

示例代码

package com.io.liushuaishuai;

import java.io.*;

public class copyFolderDemo01 {
    public static void main(String[] args) throws IOException {
        //创建数据源File对象,路径是c:\\java
        File srcfile = new File("c:\\java");
        //创建目的地File对象,路径是c:\\F
        File destfile = new File("c:\\F");

        //文件夹复制
        copyFolder(srcfile, destfile);


    }

    private static void copyFolder(File srcfile, File destfile) throws IOException {
        if (srcfile.isDirectory()) {
            String srcfileName = srcfile.getName();
            File newFolder = new File(destfile, srcfileName);
            if (!newFolder.exists()) {
                newFolder.mkdir();
            }
            File[] filearray = srcfile.listFiles();
            for (File file : filearray) {
                copyFolder(file, destfile);
            }
        } else {
            copyFile(srcfile, new File(destfile, srcfile.getName()));
        }

    }

//    jdk7以后的改进方案

    private static void copyFile(File srcfile, File destfile) {
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcfile));
             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destfile));) {


            byte[] bys = new byte[1024];
            int len;
            while ((len = bis.read(bys)) != -1) {
                bos.write(bys, 0, len);
                bos.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
//try....catch finally
    /*
    private static void copyFile(File srcfile, File destfile) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
             bis = new BufferedInputStream(new FileInputStream(srcfile));
            bos = new BufferedOutputStream(new FileOutputStream(destfile));

            byte[] bys = new byte[1024];
            int len;
            while ((len = bis.read(bys)) != -1) {
                bos.write(bys, 0, len);
                bos.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bis!=null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos!=null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
    */
    /*
private static void copyFile(File srcfile, File destfile) {


    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcfile));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destfile));

    byte[] bys = new byte[1024];
    int len;
    while ((len = bis.read(bys)) != -1) {
        bos.write(bys, 0, len);
        bos.flush();
    }


    bis.close();
    bos.close();
}*/
}

 

复制文件的异常处理

标签:stream   trace   fileinput   cto   red   image   else   directory   list   

原文地址:https://www.cnblogs.com/lsswudi/p/11429818.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!