package FileCopy;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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;
import java.util.LinkedList;
import java.util.Queue; public class CopyFileV2 {
public static void main(String agrs[]){
String srcFileName = "F:" + File.separator + "ppt";
String destFilePath = "D:" + File.separator + "copyFileExam"; // copy(srcFileName, destFilePath);
rNoCopy(srcFileName, destFilePath);
System.out.println("拷贝完成!!!"); } private static void rNoCopy(String fileName, String destFileName){ Queue<String[]> fPQ = new LinkedList<String[]>();
String[] s = new String[2];
s[0] = fileName;
s[1] = destFileName;
fPQ.offer(s); int i = 0; while(!fPQ.isEmpty()){ for(String[] st : fPQ){
for(String str : st)
System.out.println(str);
} System.out.println("\n" + i++);
String[] filePath = fPQ.poll(); File file = new File(filePath[0]);
File destFile = new File(filePath[1]); if(!destFile.exists()){
destFile.mkdirs();
System.out.println("创建多级列表成功!!!");
} File files[] = file.listFiles(); for(File f: files){
if(f.isFile()){
fileCopy(f.getPath(), filePath[1] + File.separator + f.getName());
}
else if(f.isDirectory()){
String[] temps = new String[2]; temps[0] = f.getPath();
temps[1] = filePath[1] + File.separator + f.getName(); fPQ.offer(temps);
}
}
}
} private static void copy(String fileName, String destFileName){
File file = new File(fileName);
File destFile = new File(destFileName); File files[] = file.listFiles(); if(!destFile.exists()){
destFile.mkdirs();
} for(File f: files){
if(f.isFile()){
fileCopy(f.getPath(), destFileName + File.separator + f.getName());
}
else if(f.isDirectory()){
copy(f.getPath(), destFileName + File.separator + f.getName());
}
} } private static void fileCopy(String fileName, String destFileName){
System.out.println("正在拷贝文件!!!");
File file = new File(fileName);
File destFile = new File(destFileName); // InputStream is = null;
// OutputStream os = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null; try {
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(new FileOutputStream(destFile)); byte bytes[] = new byte[1024];
while( bis.read(bytes) != -1 ){
bos.write(bytes);
bos.flush();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// os.flush();
// is.close();
// os.close();
bos.flush();
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
} // private static void getAllFileList(String fileName){
// File file = new File(fileName);
// File files[] = file.listFiles();
//
// for(File f: files){
// if(f.isFile()){
//
// }
// if(f.isDirectory()){
// getAllFileList(f.toString());
// }
// else {
// System.out.println(f.getName());
// }
// }
//
// }
}
}

IO 文件夹的拷贝的更多相关文章

  1. IO流-文件夹的拷贝

    文件夹的拷贝操作 要求: 完成文件夹的拷贝,包括子目录的拷贝和所有文件的拷贝 分析: 首先,得在目标目录下创建一个与源文件夹名称相同的文件夹 遍历源文件夹中的所有文件对象,判断子文件是目录还是文件 如 ...

  2. Java字节流实现文件夹的拷贝

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...

  3. Linux下 两台机器文件/文件夹 相互拷贝

    Linux下 两台机器文件/文件夹 相互拷贝 设有两台机器 :A:*.101及 B:*.102. 把A下的.temp/var/a.txt拷贝到B机器的/text/目录下: 进入B机器:scp root ...

  4. IO文件夹拷贝(文件内含有文件和文件夹)

    /** * 文件夹拷贝(文件内含有文件和文件夹) * * @param src * @param des */ private static void copy(String src, String ...

  5. shell脚本编写某一文件夹内拷贝某一段文件(有则跳过没有则拷贝)

    必须是同一台服务器下,或者挂载目录,不同服务器下没办法查询目录中是否有该文件 如果不在同一服务器下,可以把要查询的那个服务器的文件夹设置共享挂在到当前服务器 或者可以把脚本写到要拷贝的服务器上,那么s ...

  6. python根据列表创建文件夹,拷贝指定文件

    内容涉及:关键字定位,列表去重复,路径组装,文件夹创建,文件拷贝,字符串分割 list.txt的内容为包含关键字的文件路径,如:关键字 ’181‘ org/20190523/1/20190523201 ...

  7. python实现从文件夹随机拷贝出指定数量文件到目标文件夹

    为了方便倒腾数据,功能如题,该脚本和操作目录在同一根目录 实际运行时要手动修改程序中:cpfile_rand('img', 'outfile', 10) # 操作目录,输出目录,输出数量 import ...

  8. C++实现将一个文件夹内容拷贝至另一个文件夹

    Windows提供了非常好用的方法SHFileOperation,而且功能强大, 不光可以拷贝,还有移动.删除等等操作.直接上代码: 1 void CopyFolder(TCHAR* srcFolde ...

  9. Java_文件夹拷贝

    一.思路 * 文件夹的拷贝 1.递归查找子孙级文件 2.文件复制 文件夹创建 二.代码 package com.ahd.File; import java.io.File; import java.i ...

随机推荐

  1. php ffmpeg视频和序列帧转化

    php ffmpeg视频和序列帧转化 <pre>$cmd=shell_exec("ffmpeg -i ".__DIR__ . "/shipin1.mp4 -r ...

  2. Nginx使用反向代理实现负载均衡

    Nginx使用反向代理实现负载均衡 yls 2019-9-20 简介 基于docker容器以及docker-compose,所以需要自学docker在linux环境的基本使用 使用两个tomcat作为 ...

  3. Phone Code

    Polycarpus has n friends in Tarasov city. Polycarpus knows phone numbers of all his friends: they ar ...

  4. css字体图标的制作

    我介绍的这个办法是直接在 "阿里巴巴图标库"中制作的,方便快捷 1. 首先到 "阿里巴巴图标库"中找到你想要的图片,然后选择加入购物车 接着我们点击右上角的购物 ...

  5. PHP 高级面试115题汇总(含答案)

    1.给你四个坐标点,判断它们能不能组成一个矩形,如判断 ([0,0],[0,1],[1,1],[1,0]) 能组成一个矩形.勾股定理,矩形是对角线相等的四边形.只要任意三点不在一条直线上,任选一点,求 ...

  6. C语言|博客作业08

    这个作业属于哪个课程 C语言程序设计II 这个作业的要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-1/homework/9976 我在这个课程的 ...

  7. A Lot of Games(Trie树 + 博弈)

    题目链接:http://codeforces.com/contest/455/problem/B 题意:n, k 分别表示 字符串组数 和 比赛次数.  从一个空单词开始, a,b二人分别轮流往单词后 ...

  8. 用 GitBook 创建一本书

    用 GitBook 创建一本书 Gitbook 首先是一个软件,它使用 Git 和 Markdown 来编排书本,如果你没有听过 Git 和 Markdown,那么 gitbook 可能不适合你直接入 ...

  9. [转]shell 特殊字符

    下面这篇博文对特殊字符总结的非常齐全.这里做一下mark.另外补充一些例子. https://blog.csdn.net/K346K346/article/details/51819236 假设我们定 ...

  10. Java虚拟机的内存

    JDK1.8之前,java内存分为 线程共享区:堆.方法区.直接内存(非运行时数据区的一部分).线程私有区:程序计数器.虚拟机栈.本地方法栈. JDK1.8开始,虚拟机取消了方法区,改为元空间. 程序 ...