JAVA 复制文件夹(含子目录)
来源:互联网 发布:数据库用户是什么 编辑:程序博客网 时间:2024/05/22 05:01
/**
* 复制文件夹(含子目录)
*
* @param oldPath
* e:/temp
* @param newPath
* f:/
*/
public static void copyFolderWithSelf(String oldPath, String newPath) {
FileInputStream input = null;
FileOutputStream output = null;
try {
new File(newPath).mkdirs(); // 如果文件夹不存在 则建立新文件夹
File dir = new File(oldPath);
// 目标
newPath += dir.getName() + File.separator;
File moveDir = new File(newPath);
if (dir.isDirectory()) {
if (!moveDir.exists()) {
moveDir.mkdirs();
}
}
if (dir != null) {
String[] file = dir.list();
File temp = null;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
}
if (temp.isFile()) {
input = new FileInputStream(temp);
output = new FileOutputStream(newPath + "/" + (temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if (temp.isDirectory()) { // 如果是子文件夹
copyFolderWithSelf(oldPath + "/" + file[i], newPath);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (output != null) {
output.close();
}
if (input != null) {
input.close();
}
} catch (Exception e2) {
// TODO: handle exception
}
}
}
* 复制文件夹(含子目录)
*
* @param oldPath
* e:/temp
* @param newPath
* f:/
*/
public static void copyFolderWithSelf(String oldPath, String newPath) {
FileInputStream input = null;
FileOutputStream output = null;
try {
new File(newPath).mkdirs(); // 如果文件夹不存在 则建立新文件夹
File dir = new File(oldPath);
// 目标
newPath += dir.getName() + File.separator;
File moveDir = new File(newPath);
if (dir.isDirectory()) {
if (!moveDir.exists()) {
moveDir.mkdirs();
}
}
if (dir != null) {
String[] file = dir.list();
File temp = null;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
}
if (temp.isFile()) {
input = new FileInputStream(temp);
output = new FileOutputStream(newPath + "/" + (temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if (temp.isDirectory()) { // 如果是子文件夹
copyFolderWithSelf(oldPath + "/" + file[i], newPath);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (output != null) {
output.close();
}
if (input != null) {
input.close();
}
} catch (Exception e2) {
// TODO: handle exception
}
}
}
0 0
- JAVA 复制文件夹(含子目录)
- 文件夹及其子目录复制
- java实现文件夹的复制(含代码)
- java 文件夹及其子目录 子文件的复制
- C/C++ 遍历文件夹下的文件名(不含子目录)
- C/C++ 遍历文件夹下的文件名(含子目录)
- 一个将源目录下所有文件(含子目录)拷贝到一个新文件夹的小程序
- 用java统计一个目录(含子目录)下所有java源文件中代码行数
- 复制文件夹以及所含的文件
- IO流之复制文件夹中及其子目录中的文件
- java删除文件夹(递归删除其子目录的所有文件夹及文件)
- 统计某个目录(含子目录)下所有java源文件中实际代码行数
- java 文件剪切 含文件夹
- Linux下复制指定的目录及其子目录下的文件,到其他文件夹下(4种方法)
- Java删除文件夹以及文件夹下的子目录与文件
- Java删除文件夹以及文件夹下的子目录与文件
- java 复制文件夹
- java 复制文件/文件夹
- 读懂 Android 中的代码混淆
- Subsets II
- Android中的HttpURLConnection网络请求方式
- Ubuntu Linux 下 Ffmpeg 及 Mencoder 安装使用小结
- cocos2d-x 3.x学习之声音系统
- JAVA 复制文件夹(含子目录)
- SGU 311. Ice-cream Tycoon(平衡树)
- 欢迎使用CSDN-markdown编辑器
- This cache store does not support tagging
- 405: HTTP method GET is not supported by this URL
- 个人总结之基于MSP430F5510的X9331驱动
- C++中的类型转换static_cast、dynamic_cast、const_cast和reinterpret_cast总结
- 程序中减少使用 if 语句的策略
- RecyclerView基本用法