文件夹复制

文件夹的复制是在文件复制基础上设计的,首先判断是不是文件夹,是就创建新目录,不是就复制文件,有3个文件复制方法,都能复制文件。

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

public class FilesCopy {

    /**
     * 完成文件拷贝
     * 
     * @param source
     *            源文件
     * @param targetDir
     *            目标目录
     */
    public void copys(File source, File targetDir) {
        File target = new File(targetDir, source.getName());
        if (target.isDirectory()) {

            File[] files = source.listFiles();
            if (files != null) {
                for (File file : files) {
                    // int spaceCount = file.getAbsolutePath().split("\\\\").length;
                    // for (int i = 0; i < spaceCount; i++) {
                    // System.out.print("--");
                    // }
                    // System.out.println(file.getName());
                    if (file.isDirectory()) {
                        // find(file);
                        // target = new File(target, file.getName());
                        if (!new File(target, file.getName()).exists()) {

                            new File(target, file.getName()).mkdirs();
                        }
                        copys(file, target);
                    } else {
                        copy(file, target);
                    }
                }
            } else {

                // target.mkdirs();
            }
        }else {
            copy(source, targetDir);
        }
    }

    public static boolean MyisDirectory(String s) {
        if (s.matches(".+\\.[a-z]{3,4}")) {
            return false;
        } else {
            return true;
        }
    }

    public void fileCopy(File source, File targetDir) throws IOException {
        File target = new File(targetDir, source.getName());
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
        byte[] b = new byte[1024];

        int len = 0;
        while ((len = bis.read(b, 0, len)) != -1) {
            bos.write(b, 0, len);
            ;

        }
        bos.close();
        bis.close();
    }
    public void copy2(File source,File targetDir) throws IOException{
        File target = new File(targetDir,source.getName());
        //获取源文件的随机访问文件流
        RandomAccessFile raf_in = new RandomAccessFile(source, "r");
        //获取目标文件的随机访问文件流
        RandomAccessFile raf_out = new RandomAccessFile(target, "rw");
        byte[] b = new byte[1024];
        int len = 0;
        while((len = raf_in.read(b)) != -1){
            raf_out.write(b, 0, len);
        }
        raf_out.close();
        raf_in.close();
    }
    
    public void copy(File source, File targetDir) {
        // 根据指定的目录以及源文件名称构建新的file对象
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }
        File target = new File(targetDir, source.getName());
        InputStream is = null;
        OutputStream os = null;
        try {
            // 创建源文件的输入流
            is = new FileInputStream(source);
            // 创建目标文件的输出流
            os = new FileOutputStream(target);
            // 声明字节缓冲区
            byte[] b = new byte[1024];
            // 声明临时变量存储每次读取的真实长度
            int len = 0;
            System.out.println("开始拷贝...");
            while ((len = is.read(b)) != -1) {

                os.write(b, 0, len);
            }
            System.out.println("拷贝完成!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args) {
        File dir1 = new File("C:\\Users\\Administrator\\Desktop\\刘庆.zip");
        File dir2 = new File("C:\\Users\\Administrator\\Desktop\\3");
        new FilesCopy().copys(dir1, dir2);
    }
}
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值