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. Arduino 处理JSON格式的数据

    Arduino 处理JSON格式的数据 1.安装 ArduinoJson这个包 2.程序代码 # include <ArduinoJson.h> #define ALINK_BODY_FO ...

  2. MySQL如何永久解决由dos编码格式导致MySQ的显示乱码

    MySQL如何永久解决由dos编码格式导致MySQ的显示乱码 Ⅰ.新建文件 changeCode.txt Ⅱ.粘贴下面代码(作用:将默认的GBK(936)修改为UTF-8(65001)) Window ...

  3. nyoj 28-大数阶乘 (大数模板)

    28-大数阶乘 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:19 submit:39 题目描述: 我们都知道如何计算一个数的阶乘,可是,如果这个数 ...

  4. Blob字段

    在oracle中有一个特殊的字段类型Blob,Blob是指二进制大对象也就是英文Binary Large Object的缩写: 通过sql取出后,需要进行特殊处理.而我是这样玩的: 1.通过sql查询 ...

  5. Apache服务安装及一些基本操作

    注意:安装apache服务之前记得搭建yum仓库 1.安装apache服务,输入命令“yum install httpd” 安装成功后,会这样显示 2.需要对Apache服务进行启动,输入命令“sys ...

  6. nginx配置路径问题

    编译了一个程序放在服务器上,通过nginx配置转发访问.例如在配置下图的地址 d:\wayne\nginxWeb\www: 发现无法正常运行,查看error.log发现是有问题的,当创建文件时,ngi ...

  7. 面试官:关于Java性能优化,你有什么技巧

    通过使用一些辅助性工具来找到程序中的瓶颈,然后就可以对瓶颈部分的代码进行优化. 一般有两种方案:即优化代码或更改设计方法.我们一般会选择后者,因为不去调用以下代码要比调用一些优化的代码更能提高程序的性 ...

  8. 2016 校招, Android 开发,一个本科应届的坎坷求职之路(转)

    转载出处:http://www.nowcoder.com/discuss/3244?type=2&order=0&pos=1&page=1 和大多数的面经不同,我不是大牛,手头 ...

  9. 【论文阅读】Binary Multi-View Clustering

    文章地址:https://ieeexplore.ieee.org/document/8387526 出自:IEEE Trans. on Pattern Analysis and Machine Int ...

  10. 解决failed to push some refs to 'git@github.com:TQBX/GIT-Github-.git'问题

    解决以下问题问题: git pull origin master --allow-unrelated-histories 进入vim界面->ESC->:wq 重复第一步->git p ...