IO 文件夹的拷贝
2024-08-29 03:20:29
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());
// }
// }
//
// }
}
}
最新文章
- CentOS 搭建openVPN
- 玩转Windows服务系列——Windows服务启动超时时间
- Hive启动报错: Found class jline.Terminal, but interface was expected
- Joomla软件功能介绍与开源程序大比拼Joomla,wordpress,Drupal哪个好?
- noip2008 双栈排序
- <;转>;删除文件夹下所有的.svn文件
- SqlServer 2008 R2定时备份数据库,并且发送邮件通知
- Zabbix 监控 Nginx 状态
- C++学习19 类的多继承
- Twitter Storm如何保证消息不丢失
- Oracle SQL 劈开字符串
- MVVM模式应用 之为ApplicationBarIconButton 添加Command操作属性
- HDU 2570:迷瘴
- 【Ubuntu 16】深入Ubuntu文件系统
- linux下一键安装redis并设置为后台进程及开机启动
- ch.ethz.ssh2.Session和com.jcraft.jsch.Session
- Laravel 5.2--改变数据库字段值,编辑时候,默认选中
- 理解HTTP之keep-alive(转)
- 【ARTS】01_05_左耳听风-20181210~1216
- Storm工作流程
热门文章
- 路径操作OS模块和Path类(全)一篇够用!
- 产品分析:华为短信APP体验的问题和建议
- MySQL开发规范与使用技巧总结
- [复现论文程序图]High Speed Continuous Variable Source-Independent Quantum Random Number Generation
- windows 激活工具链接
- SpringBoot 源码解析 (六)----- Spring Boot的核心能力 - 内置Servlet容器源码分析(Tomcat)
- pat 1092 To Buy or Not to Buy(20 分)
- hdu 1874 畅通工程续 (floyd)
- 领扣(LeetCode)两个数组的交集II 个人题解
- mysql通俗易懂的数据库连接池原理及模拟实现