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. 利用appium-1.5.3.dmg安装Appium. doctors时,提示 Could not detect Mac OS X Version from sw_vers output: '10.12'

    发生这种错误的原因是因为:appium不支持mac 10.12版本. 解决方法: https://stackoverflow.com/questions/40129794/how-to-fix-err ...

  2. CI_SMOKE配置手册

    1.1.  SVN安装 安装TortoiseSVN,并检出AutoScript目录至本地 1.2.  Java环境安装 确认测试环境安装了JDK,在cmd下键入java -version 检查JDK是 ...

  3. 【dfs】POJ2386湖计数

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34735   Accepted: 17246 D ...

  4. WebPack-Loader

    Loaders 鼎鼎大名的Loaders登场了! 1.什么是loaders Loaders是webpack中最让人激动人心的功能之一了.通过使用不同的loader,webpack通过调用外部的脚本或工 ...

  5. selenium获取动态网页信息(某东)-具体配置信息

    需要安装的包: selenium 关于软件的驱动:selenium之 驱动环境配置chrome.firefox.IE 1 # encoding:utf-8 2 # Author:"richi ...

  6. C#学习笔记-迭代器模式

    什么是迭代器模式? 迭代器模式(Iterator):提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示. 何时使用迭代器模式? 当需要访问一个聚合对象,而且不管这些对象是什么都需要 ...

  7. I2C通讯协议

    1.基本概念 主机            初始化发送,产生时钟信号和终止发送的器件 从机             被主机寻址的器件  发送器          发送数据到总线的器件 接收器       ...

  8. MySQL优化 - 索引优化

    索引(在MySQL中也叫做"键(key)")是存储引擎用于快速找到记录的一种数据结构. 索引对于良好的性能非常关键,尤其是当表的数据量越来越大时,索引对性能(查询)的影响愈发重要. ...

  9. 树莓派搭建WEB服务器

    树莓派搭建WEB的教程网上有许多,但感觉每一篇都有一些问题,这次我将网上的教程汇总,并亲身实践,将注意的问题都写进去,方便新手学习! 目录:1,安装nginx+sqlite+php5打造轻量级服务器, ...

  10. [51nod1425]减减数

    初始给定一个整数n.每次可以对其做一个操作,这个操作是将n减去他其中的某一位.得到新的一个数字n',然后继续操作,直到他变成0为止. 比如24这个例子,24 → 20 → 18 → 10 → 9 → ...