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类实现文件夹和文件复制的更多相关文章

  1. IO流-获取指定目录下文件夹和文件对象【File类】

    一.运用File类实现获取指定目录下文件夹和文件对象 1.File类 2.方法: 获取文件绝对路径 :getAbsolutePath 案例: import java.io.File; /** * 获取 ...

  2. Java File类应用:递归遍历文件夹和递归删除文件

    要求: 1)采用递归遍历文件夹下的所有文件,包括子文件夹下的文件 2)采用递归删除文件下的所有文件 注意: 以下递归删除文件的方法,只能删除文件,所有的文件夹都还会存在 若要删除正文文件夹,可以在递归 ...

  3. centos彻底删除文件夹、文件命令(centos 新建、删除、移动、复制等命令)

    centos彻底删除文件夹.文件命令(centos 新建.删除.移动.复制等命令: 1.新建文件夹 mkdir 文件名 新建一个名为test的文件夹在home下 view source1 mkdir ...

  4. C# 将文件夹中文件复制到另一个文件夹

    p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...

  5. OpenCV2类批量处理文件夹及文件图像 及批量处理后保存到txt文件

    //采用windows控制台实现计算文件夹中对象总数以及批量读取对象 //#include <afx.h> //和windows.h是一样的作用 #include <opencv2/ ...

  6. 转发:centos彻底删除文件夹、文件命令(centos 新建、删除、移动、复制等命令)

    http://blog.csdn.net/lpdx111/article/details/16877725 centos彻底删除文件夹.文件命令(centos 新建.删除.移动.复制等命令: 1.新建 ...

  7. java基础 File与递归练习 使用文件过滤器筛选将指定文件夹下的小于200K的小文件获取并打印按层次打印(包括所有子文件夹的文件) 多层文件夹情况统计文件和文件夹的数量 统计已知类型的数量 未知类型的数量

    package com.swift.kuozhan; import java.io.File; import java.io.FileFilter; /*使用文件过滤器筛选将指定文件夹下的小于200K ...

  8. Java IO实现文件(及文件夹)的复制 原创代码【精】

    单个文件复制 FileInputStream input=new FileInputStream("C://360//fay.jpg"); FileOutputStream out ...

  9. PHP 文件夹操作「复制、删除、查看大小」递归实现

    PHP虽然提供了 filesize.copy.unlink 等文件操作的函数,但是没有提供 dirsize.copydir.rmdirs 等文件夹操作的函数(rmdir也只能删除空目录).所以只能手动 ...

  10. PHP 文件夹操作「复制、删除、查看大小、重命名」递归实现

    PHP虽然提供了 filesize.copy.unlink 等文件操作的函数,但是没有提供 dirsize.copydir.rmdirs 等文件夹操作的函数(rmdir也只能删除空目录).所以只能手动 ...

随机推荐

  1. input 光标在 chrome下不兼容 解决方案

    input 光标在 chrome下不兼容 解决方案 height: 52px; line-height: normal; line-height:52px\9 .list li input[type= ...

  2. 数据结构-二叉树(应用篇)-之二叉搜索树 C和C++的实现

    一.概念 二叉搜索树(Binary Sort Tree/Binary Search Tree...),是二叉树的一种特殊扩展.也是一种动态查找表. 在二叉搜索树中,左子树上所有节点的均小于根节点,右子 ...

  3. 五分钟学习React(二):我的第一个Hello World

    我的第一个React应用 接着我们上一期所讲的内容,通过create-react-app脚手架创建的应用,它是基于ES6的语法生成的.我们清空src目录下的文件,并分别创建index.js和index ...

  4. WebService服务(转)

    一.序言 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是Web ...

  5. 基于Xilinx FPGA的视频图像采集系统

    本篇要分享的是基于Xilinx FPGA的视频图像采集系统,使用摄像头采集图像数据,并没有用到SDRAM/DDR.这个工程使用的是OV7670 30w像素摄像头,用双口RAM做存储,显示窗口为320x ...

  6. HDU1312-Red and Black-DFS

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. Bellman-Ford 求含负权最短路

    该算法详解请看   https://www.cnblogs.com/tanky_woo/archive/2011/01/17/1937728.html 单源最短路   当图中存在负权边时 迪杰斯特拉就 ...

  8. POJ 2531 暴力深搜

    Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13494   Accepted: 6543 ...

  9. hdu_4497GCD and LCM(合数分解)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 GCD and LCM Time Limit: 2000/1000 MS (Java/Other ...

  10. Red and Black(dfs水)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...