java基础之文件(夹)复制到另一个文件夹

文件或文件夹复制到另一个文件夹
public class FileCopy {
    public static void main(String[] args) {
        File sourceFile = new File("e:\\javaIO");
        File targetFile = new File("e:\\javaFile");// 目标文件
        System.out.println("共复制以下文件!");
        copyDirectory(sourceFile, targetFile);
    }
    private static void copyDirectory(File sourceFile, File targetFile) {
        if (sourceFile.isFile()) {// 如果是文件,则直接复制
            copyFile(sourceFile, new File(targetFile, sourceFile.getName()));
            System.out.println(sourceFile.getName()+"拷贝完成");
        } else {//如果是目录,则遍历
            File file = new File(targetFile, sourceFile.getName());//创建子文件夹
            file.mkdirs();
            System.out.println(file.getName()+"目录创建完成!");
            File[] files = sourceFile.listFiles();
            for (File file2 : files) {
                copyDirectory(file2, file);
            }
        }
    }

=================copyFile方法========================

  • 方法一:用RandomAccessFile
private static void copyFile(File sourceFile, File targetDir) {
    // TODO Auto-generated method stub
    RandomAccessFile rafSource = null;
    RandomAccessFile rafTarget =null;
    try {
        rafSource = new RandomAccessFile(sourceFile, "r");
        rafTarget = new RandomAccessFile(targetDir, "rw");
        rafTarget.setLength(sourceFile.length());
        byte[] buff = new byte[1024*4];
        int length = 0;
        while((length=rafSource.read(buff)) != -1){
            rafTarget.write(buff, 0, length);
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        IOUtils.closeRandomAccessFile(rafTarget);
        IOUtils.closeRandomAccessFile(rafSource);
    }
}
  • 方法二 IO流
public static void copyFile(File sourceFile, File targetFile) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(sourceFile));
            bos = new BufferedOutputStream(new FileOutputStream(targetFile));
            byte[] buff = new byte[1024];
            int length;
            while (-1 != (length = bis.read(buff))) {
                bos.write(buff, 0, length);
            }
            bos.flush();
        } catch (FileNotFoundException e) {
            System.out.println("路径不存在");
            System.exit(-1);
        } catch (IOException e) {
            System.out.println("文件读写错误");
            System.exit(-1);
        } finally {
            IOUtils.closeInputStream(bis);
            IOUtils.closeOutputStream(bos);
        }
    }
}
IOUtils工具类…..

border="0" width="330" height="86" src="//music.163.com/outchain/player?type=2&id=36190361&auto=1&height=66">
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值