10 文件|文件夹 拷贝方法封装

package io.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtil {
    
    /*
     * 文件的拷贝
     * @param 源文件的路径
     * @param 目标文件路径
     * @throws Exception
     * @return
     */
    public static void copyFile(String srcPath,String destPath) throws Exception {
        //1 建立联系 :  File对象 (源头----目的地)
        //源头文件必须存在,目的地文件可以不存在
        File src = new File(srcPath);
        File dest = new File(destPath);
        
        //下面注释的代码重复,可直接调用方法,无须重写
        copyFile(src,dest);
        /*//2  选择流
        InputStream is = new FileInputStream(src);
        FileOutputStream os = new FileOutputStream(dest);
        
        //3  文件拷贝(循环+读取+写出)
        byte[] flush = new byte[1024];
        int length;
        
        //读取
        while((length = is.read(flush)) != -1){
            //写出
            os.write(flush, 0, length);
        }
        os.close();
        is.close();*/
    }
    
    /*
     * 文件的拷贝
     * @param 源文件的File对象
     * @param 目标文件File对象
     * @throws Exception
     * @return
     */
    public static void copyFile(File src,File dest) throws Exception {
        if(! src.isFile()){
            System.out.println("只能拷贝文件");
            throw new IOException("只能拷贝文件");
        }
        
        //如果dest为已经存在的文件夹,不能建立与文件夹同名的文件
        if(dest.isDirectory()){
            System.out.println("不能建立与文件夹同名的文件");
            throw new IOException("不能建立与文件夹同名的文件");
        }
        //2  选择流
        InputStream is = new BufferedInputStream(
                new FileInputStream(src));
        
        OutputStream os = new BufferedOutputStream(
                new FileOutputStream(dest));
        
        //3  文件拷贝(循环+读取+写出)
        byte[] flush = new byte[1024];
        int length;
        
        //读取
        while((length = is.read(flush)) != -1){
            //写出
            os.write(flush, 0, length);
        }
        os.close();
        is.close();
    }
    
    /*
     * 拷贝文件夹
     * @param src 源File对象
     * @param des 目标File对象
     */
    public static void copyDir(File src,File dest){
        /*
         * 根据 
         *    parent(destPath)路径名字符串
         * 和 
         *    child(src.getName()) 路径名字符串
         * 创建一个新 File实例
         * (就是在destPath目录下创建名称为src.getName(的文件夹))
         */
        if(src.isDirectory()){
            dest = new File(dest,src.getName());
        }
        copyDirDetail(src,dest);
    }

    /*
     * 拷贝文件夹
     * @param src 源路径字符串
     * @param des 目标路径字符串
     */
    public static void copyDir(String srcPath,String destPath){
        File src = new File(srcPath);
        File dest = new File(destPath);
        
        copyDir(src,dest);
    }
    
    /*
     * 拷贝文件夹细节
     */
    public static void copyDirDetail(File src ,File dest){
        if(src.isFile()){  //文件
            try {
                FileUtil.copyFile(src, dest);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(src.isDirectory()){  //文件夹
            //确认目标文件夹存在
            dest.mkdirs();
            
            //获取下一级目录|文件
            for(File sub:src.listFiles()){
                copyDirDetail(sub,new File(dest,sub.getName()));
            }
        }
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容