java 复制文件夹到指定目录

好久之前学得了,当时实现了还激动的不得了。今天再拿起来 发现忘记了不少,没办法项目需要,又对流了解了下,古人说的没错啊,果然温故而知新,看来还是要常回头啊。

下面说下 复制文件夹的重难点(其实不是太难了,给我个装大神的机会吧,哈哈)

当然了,首先 肯定是要对IO流有一定的了解,不然看代码会很懵逼的。其次 是对逻辑要有一定的概念,就是对文件夹和文件的不同处理方式。这个我就在这里说下 文件夹要进行回调处理(新手看代码可能会觉得,文件夹执行一半时再回调执行子文件夹,这样为什么到头来还会把父文件夹中未处理的处理完成。哈哈,这就是回调函数的魅力啊,好好看看吧,很神奇的),文件的话 就是直接创建流进行读写操作,在这里要了解BufferedInputStream、BufferedOutputStream用于不用的特点以及和FileInputStream、FileOutputStream之间的关系。读写的操作都是固定的,记住就好,当然理解更好了,可以长久记忆(毕竟强行记忆不是咱智商优势程序员的风格)。最后 也是很重要的一点,千万不要忘记关闭流(不关的话,结果会令你哭泣)


废话不多说,用码说话:

package com.tpad.copydir;

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 javax.swing.JOptionPane;

public class CopyDir {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
long startTime = System.currentTimeMillis(); // 开始执行的时间
String srcDirFullPath = "d:\\floder";
String dstDirFullPath = "e:\\copy_flode\\a\\b\\c";

try {
CopyDir copydir = new CopyDir();
copydir.CopyFloder(srcDirFullPath, dstDirFullPath);
JOptionPane.showMessageDialog(null, "ok");
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}

long endTime = System.currentTimeMillis(); // 执行完的时间
long useTime = endTime - startTime; // 执行复制文件夹用的时间(毫秒)
System.out.println("完成复制共用" + useTime + "毫秒");
}

public void CopyFloder(String srcDirFullPath, String dstDirFullPath) throws IOException {
// TODO Auto-generated method stub
File srcFile = new File(srcDirFullPath);
File dstFile = new File(dstDirFullPath);

File[] file = srcFile.listFiles(); // 获取文件目录内的文件个数

// 目标文件是否存在
if (!dstFile.exists()) {
if (dstFile.mkdirs() == false) { // 这里是先创建然后验证是否创建成功,成功 true  失败 false
throw new IOException(dstFile.getPath() + " (The source file creation failed)"); // 失败 给用户提示
}
}
// 遍历
for (File newFile : file) {
if (newFile.isDirectory()) {
// 文件夹
File sendFloderFile = new File(dstFile, newFile.getName());
// 在目标地址创建文件夹
if (!sendFloderFile.exists()) {
if (sendFloderFile.mkdir() == false) {
throw new IOException(sendFloderFile + " (File creation failed)");
}
}
// 回调, 即子文件夹进行处理
CopyFloder(newFile.toString(), sendFloderFile.toString());
} else {
// 文件
File sendWordFile = new File(dstFile, newFile.getName());
CopyWord(newFile.toString(), sendWordFile.toString());
}
}
}

// 对文件进行读写操作
public void CopyWord(String srcDirFullPath2, String dstDirFullPath2) throws IOException {
// TODO Auto-generated method stub
File srcFile2 = new File(srcDirFullPath2);
File dstFile2 = new File(dstDirFullPath2);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile2));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dstFile2));
int ch = 0;
byte[] byteArray = new byte[1024];
while ((ch = bis.read(byteArray)) != -1) {
bos.write(byteArray, 0, ch);
}
bos.close();
bis.close();
}
}

有错误,欢迎大家指正哈,共同进步

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值