File类实现文件夹和文件复制
package com.jcy.copy; 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; public class FolderAllCopy { /**
* 复制文件夹
*
* @param oldPath
* 被复制的文件夹
* @param newPath
* 要复制到的文件夹
*/
public static void copyFolder(String oldPath, String newPath) { File newFile = new File(newPath); File oldFile = new File(oldPath); if (!oldFile.isDirectory()) {// 判断是不是文件夹
copyFile(oldPath, newPath);
return;
}
// 获得复制文件夹路径的字符串长度
int len = oldPath.length();
// 得到该文件夹的所有文件和文件夹
File[] files = oldFile.listFiles();
copy(files, newPath, len);
} private static void copy(File[] files, String newPath, int len) {
if (files == null) {
return;
}
for (File file : files) {
if (file.isDirectory()) {// 是否为文件夹
copy(file.listFiles(), newPath, len);
}
// System.out.println(file.getName());
// eg: newPath : f:\\ss
// oldPath : d:\\ww --> len
// file : d:\\ww\\ee\\d.txt
// ==> \\ee\\d.txt
// ==> newPath + path f:\\ss\\ee\\d.txt
String path = file.getAbsolutePath().substring(len,
file.getAbsolutePath().length()); if (file.isFile()) {// 是否为文件
copyFile(file.getPath(), newPath + path);
} } } /**
* 单个文件复制
*
* @param oldPath
* 复制的文件路径
* @param newPath
* 复制到的目标地
* @return
*/
public static boolean copyFile(String oldPath, String newPath) { //System.out.println(oldPath);
//System.out.println(newPath);
File oldFile = new File(oldPath); if (!oldFile.exists()) {
System.out.println("源文件不存在。" + oldFile.exists());
return false;
}
if (!oldFile.canRead()) {
System.out.println("源文件不可以读");
return false;
} // 得到文件和文件夹的名
String fileName = oldFile.getName();
String[] strs = fileName.split("\\.");
// 对目标路径进行处理
if (!newPath.endsWith("." + strs[strs.length - 1])) {// 传入的路径不是 .XXX
if (newPath.endsWith("\\")) {// 传入的路径是文件夹 \\
newPath += fileName;
} else {
newPath += File.separator + fileName;
}
}
if (!oldFile.isFile()) {
return false;
}
File newFile = new File(newPath); // 验证新路径的文件夹是否存在
if (!newFile.getParentFile().exists()) {
// 文件夹不存在时,创建文件夹
newFile.getParentFile().mkdirs();
} InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(oldFile);
out = new FileOutputStream(newFile);
byte[] b = new byte[1024];
int temp = 0;
while ((temp = in.read(b)) != -1) {
out.write(b, 0, temp);
}
return true; } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
} public static void main(String[] args) {
FolderAllCopy.copyFile("D:\\info.sql", "d:\\srcll\\23\\ww1\\1.sql");
FolderAllCopy.copyFolder("D:\\Note", "D:\\srcll\\qq");
} }
File类实现文件夹和文件复制的更多相关文章
- IO流-获取指定目录下文件夹和文件对象【File类】
一.运用File类实现获取指定目录下文件夹和文件对象 1.File类 2.方法: 获取文件绝对路径 :getAbsolutePath 案例: import java.io.File; /** * 获取 ...
- Java File类应用:递归遍历文件夹和递归删除文件
要求: 1)采用递归遍历文件夹下的所有文件,包括子文件夹下的文件 2)采用递归删除文件下的所有文件 注意: 以下递归删除文件的方法,只能删除文件,所有的文件夹都还会存在 若要删除正文文件夹,可以在递归 ...
- centos彻底删除文件夹、文件命令(centos 新建、删除、移动、复制等命令)
centos彻底删除文件夹.文件命令(centos 新建.删除.移动.复制等命令: 1.新建文件夹 mkdir 文件名 新建一个名为test的文件夹在home下 view source1 mkdir ...
- C# 将文件夹中文件复制到另一个文件夹
p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...
- OpenCV2类批量处理文件夹及文件图像 及批量处理后保存到txt文件
//采用windows控制台实现计算文件夹中对象总数以及批量读取对象 //#include <afx.h> //和windows.h是一样的作用 #include <opencv2/ ...
- 转发:centos彻底删除文件夹、文件命令(centos 新建、删除、移动、复制等命令)
http://blog.csdn.net/lpdx111/article/details/16877725 centos彻底删除文件夹.文件命令(centos 新建.删除.移动.复制等命令: 1.新建 ...
- java基础 File与递归练习 使用文件过滤器筛选将指定文件夹下的小于200K的小文件获取并打印按层次打印(包括所有子文件夹的文件) 多层文件夹情况统计文件和文件夹的数量 统计已知类型的数量 未知类型的数量
package com.swift.kuozhan; import java.io.File; import java.io.FileFilter; /*使用文件过滤器筛选将指定文件夹下的小于200K ...
- Java IO实现文件(及文件夹)的复制 原创代码【精】
单个文件复制 FileInputStream input=new FileInputStream("C://360//fay.jpg"); FileOutputStream out ...
- PHP 文件夹操作「复制、删除、查看大小」递归实现
PHP虽然提供了 filesize.copy.unlink 等文件操作的函数,但是没有提供 dirsize.copydir.rmdirs 等文件夹操作的函数(rmdir也只能删除空目录).所以只能手动 ...
- PHP 文件夹操作「复制、删除、查看大小、重命名」递归实现
PHP虽然提供了 filesize.copy.unlink 等文件操作的函数,但是没有提供 dirsize.copydir.rmdirs 等文件夹操作的函数(rmdir也只能删除空目录).所以只能手动 ...
随机推荐
- js的onscroll、scrollTop、scrollHeight及window.scroll等方法
onscroll 解释:当元素的滚动条滚动时触发的事件. onscroll事件貌似任何实体元素都可以绑定,这里的实体元素包括DOM元素.window元素.document元素. 用法即:element ...
- 关于dl dt dd 文字过长换行在移动端显示对齐的探讨总结
关于dl dt dd 文字过长换行在移动端显示对齐的探讨总结 <dl> <dt>抵押房产:</dt> <dd>1.北京市大兴区兴华大街丽园小区3单大兴 ...
- 2017 年的 人生 hard 模式终于结束了,2018年回归初心
2017 年的 人生 hard 模式终于结束了,2018年回归初心 2017年对于我个人来讲, 毫不夸张的说 算是近十年来除了高考那一年,最最惊心动魄的一年了,没有之一. >>>开篇 ...
- asp.net mvc 防止重复提交
string hiddenToken = "hiddenToken"; public void OnActionExecuting(ActionExecutingContext f ...
- hook 虚表
PVOID* GetVtpl(PVOID lpThis, int nIndex){ return *(PVOID**)lpThis + nIndex;} PVOID HookVtpl(PVOID* ...
- Jenkins-Dingding Notification Plugin 配置
具体内容请移步Jenkins官网或作者Github,本文仅为实践分享,记录几个小坑! 感谢插件作者的贡献! 转载注明出处:http://www.cnblogs.com/ityoung/p/648915 ...
- 微信小程序——获取openid
1.在微信小程序后台得到appid.AppSecret 2.在任意小程序界面的[onLoad]中取得code(建议将代码写在index.js中) 3.用取得code换取openid 如后台无法将ope ...
- 关于mybatis-generator的问题
1.运行完mybatis-generator没有出现问题,但是代码看不到,出现这种东西: 你需要使用相对路径,如项目名+/src/main/java,就可以解决了 2.附录我的代码以供参考: < ...
- HDU 1024 Max Sum Plus Plus【动态规划求最大M子段和详解 】
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Max Sum(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others) ...