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. Docker三十分钟快速入门(下)

    一.背景 上篇文章我们进行了Docker的快速入门,基本命令的讲解,以及简单的实战,那么本篇我们就来实战一个真实的项目,看看怎么在产线上来通过容器技术来运行我们的项目,来达到学会容器间通信以及dock ...

  2. Linux CentOS 6.5 配置网络

    网卡说明 第一块网卡为配置外网:eth0 第二块网卡为配置内网:eth1(没有外网的机器也要将内网配置在第二块网卡上) 1.使用ifconfig查看网卡配置信息 2.修改网卡1配置文件/etc/sys ...

  3. oracle之 SYSAUX表空间维护

    1.查询表空间使用率SQL> set linesize 400SQL> set pagesize 400SQL> SELECT D.TABLESPACE_NAME 表空间名称,SPA ...

  4. Vim 在 windows 环境下的初步配置

    一..下载工具包 vim 程序:下载安装程序 vim-plug 插件: 下载插件管理文件 二. 安装 VIM 1.如果无特别要求,一路选择默认, 在类型选择时,选择 full 2. 配置 vim-pl ...

  5. ES6小点心第二弹——底部浮现弹窗

    小点心,顾名思义,开箱即食,拿来即用. 献上第二个小点心:SlidePopup. GitHub 在线演示 GitHub 上欢迎大家来找茬^_^ 前端朋友们,今天要介绍的这款小点心牛B了.相信是个前端都 ...

  6. RHM-M10汽车吊力矩限制器/载荷指示器

    一 产品特点 1.     采用7.0寸工业65K色TFT LCD真彩屏,亮度250nit,分辨率800×480: 2.     传感器采用进口机芯,过载能力强: 3.     采用油压取力和大臂弯曲 ...

  7. ZOJ 1403&&HDU 1015 Safecracker【暴力】

    Safecracker Time Limit: 2 Seconds      Memory Limit: 65536 KB === Op tech briefing, 2002/11/02 06:42 ...

  8. 基于Windows下python3.4.1IDLE常用快捷键小结

    安装IDLE后鼠标右键点击*.py 文件,可以看到Edit with IDLE 选择这个可以直接打开编辑器.IDLE默认不能显示行号,使用ALT+G 跳到对应行号,在右下角有显示光标所在行.列.ALT ...

  9. ACM_素数筛选

    /* *素数筛法,判断小于MAXN的数是不是素数. *notprime是一张表,为false表示是素数,true表示不是素数 */ const int MAXN = 1000010; bool not ...

  10. FineReport父子格实现动态参数注入

    "深入学习FineReport后发现其功能及其强大,之前使用存储过程实现的报表完全可以使用FineReport本身的功能实现. 当你需要的表名,查询条件等均未知的时候,使用"动态参 ...