package com.sitech.message.controller.task;
import java.io.File;//引入类
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

//实现文件的简单处理,复制和移动文件、目录等
public class TextCopyFileAndMove {
public static void fileMove(String from, String to) throws Exception {// 移动指定文件夹内的全部文件
try {
File dir = new File(from);
File[] files = dir.listFiles();// 将文件或文件夹放入文件集
if (files == null)// 判断文件集是否为空
return;
File moveDir = new File(to);// 创建目标目录
if (!moveDir.exists()) {// 判断目标目录是否存在
moveDir.mkdirs();// 不存在则创建
}
for (int i = 0; i < files.length; i++) {// 遍历文件集
if (files[i].isDirectory()) {// 如果是文件夹或目录,则递归调用fileMove方法,直到获得目录下的文件
fileMove(files[i].getPath(), to + "\\" + files[i].getName());// 递归移动文件
files[i].delete();// 删除文件所在原目录
}
File moveFile = new File(moveDir.getPath() + "\\"// 将文件目录放入移动后的目录
+ files[i].getName());
if (moveFile.exists()) {// 目标文件夹下存在的话,删除
moveFile.delete();
}
files[i].renameTo(moveFile);// 移动文件
System.out.println(files[i] + " 移动成功");
}
} catch (Exception e) {
throw e;
}
}

// 复制目录下的文件(不包括该目录)到指定目录,会连同子目录一起复制过去。
public static void copyFileFromDir(String toPath, String fromPath) {
File file = new File(fromPath);
createFile(toPath, false);// true:创建文件 false创建目录
if (file.isDirectory()) {// 如果是目录
copyFileToDir(toPath, listFile(file));
}
}

// 复制目录到指定目录,将目录以及目录下的文件和子目录全部复制到目标目录
public static void copyDir(String toPath, String fromPath) {
File targetFile = new File(toPath);// 创建文件
createFile(targetFile, false);// 创建目录
File file = new File(fromPath);// 创建文件
if (targetFile.isDirectory() && file.isDirectory()) {// 如果传入是目录
copyFileToDir(targetFile.getAbsolutePath() + "/" + file.getName(),
listFile(file));// 复制文件到指定目录
}
}

// 复制一组文件到指定目录。targetDir是目标目录,filePath是需要复制的文件路径
public static void copyFileToDir(String toDir, String[] filePath) {
if (toDir == null || "".equals(toDir)) {// 目录路径为空
System.out.println("参数错误,目标路径不能为空");
return;
}
File targetFile = new File(toDir);
if (!targetFile.exists()) {// 如果指定目录不存在
targetFile.mkdir();// 新建目录
} else {
if (!targetFile.isDirectory()) {// 如果不是目录
System.out.println("参数错误,目标路径指向的不是一个目录!");
return;
}
}
for (int i = 0; i < filePath.length; i++) {// 遍历需要复制的文件路径
File file = new File(filePath[i]);// 创建文件
if (file.isDirectory()) {// 判断是否是目录
copyFileToDir(toDir + "/" + file.getName(), listFile(file));// 递归调用方法获得目录下的文件
System.out.println("复制文件 " + file);
} else {
copyFileToDir(toDir, file, "");// 复制文件到指定目录
}
}
}

public static void copyFileToDir(String toDir, File file, String newName) {// 复制文件到指定目录
String newFile = "";
if (newName != null && !"".equals(newName)) {
newFile = toDir + "/" + newName;
} else {
newFile = toDir + "/" + file.getName();
}
File tFile = new File(newFile);
copyFile(tFile, file);// 调用方法复制文件
}

public static void copyFile(File toFile, File fromFile) {// 复制文件
if (toFile.exists()) {// 判断目标目录中文件是否存在
System.out.println("文件" + toFile.getAbsolutePath() + "已经存在,跳过该文件!");
return;
} else {
createFile(toFile, true);// 创建文件
}
System.out.println("复制文件" + fromFile.getAbsolutePath() + "到"
+ toFile.getAbsolutePath());
try {
InputStream is = new FileInputStream(fromFile);// 创建文件输入流
FileOutputStream fos = new FileOutputStream(toFile);// 文件输出流
byte[] buffer = new byte[1024];// 字节数组
while (is.read(buffer) != -1) {// 将文件内容写到文件中
fos.write(buffer);
}
is.close();// 输入流关闭
fos.close();// 输出流关闭
} catch (FileNotFoundException e) {// 捕获文件不存在异常
e.printStackTrace();
} catch (IOException e) {// 捕获异常
e.printStackTrace();
}
}

public static String[] listFile(File dir) {// 获取文件绝对路径
String absolutPath = dir.getAbsolutePath();// 声获字符串赋值为路传入文件的路径
String[] paths = dir.list();// 文件名数组
String[] files = new String[paths.length];// 声明字符串数组,长度为传入文件的个数
for (int i = 0; i < paths.length; i++) {// 遍历显示文件绝对路径
files[i] = absolutPath + "/" + paths[i];
}
return files;
}

public static void createFile(String path, boolean isFile) {// 创建文件或目录
createFile(new File(path), isFile);// 调用方法创建新文件或目录
}

public static void createFile(File file, boolean isFile) {// 创建文件
if (!file.exists()) {// 如果文件不存在
if (!file.getParentFile().exists()) {// 如果文件父目录不存在
createFile(file.getParentFile(), false);
} else {// 存在文件父目录
if (isFile) {// 创建文件
try {
file.createNewFile();// 创建新文件
} catch (IOException e) {
e.printStackTrace();
}
} else {
file.mkdir();// 创建目录
}
}
}
}

public static void main(String[] args) {// java程序主入口处
String fromPath = "E:/createFile/shafei.log";// 目录路径
String toPath = "F:/createFile/shafei.log";// 源路径
System.out.println("1.移动文件:从路径 " + fromPath + " 移动到路径 " + toPath);
File fromPaths = new File("F:/createFile/createFile/test.log");
File tofiles = new File("F:/createFile/shafei/test.log");
copyFile(tofiles,fromPaths);
try {
fileMove(fromPath, toPath);// 调用方法实现文件的移动
} catch (Exception e) {
System.out.println("移动文件出现问题" + e.getMessage());
}
System.out.println("2.复制目录 " + toPath + " 下的文件(不包括该目录)到指定目录" + fromPath
+ " ,会连同子目录一起复制过去。");
copyFileFromDir(fromPath, toPath);// 调用方法实现目录复制
System.out.println("3.复制目录 " + fromPath + "到指定目录 " + toPath
+ " ,将目录以及目录下的文件和子目录全部复制到目标目录");
copyDir(toPath, fromPath);// 调用方法实现目录以用目录下的文件和子目录全部复制
}
}

最新文章

  1. mac 终端常用命令
  2. 论文阅读(Xiang Bai——【CVPR2012】Detecting Texts of Arbitrary Orientations in Natural Images)
  3. oracleclient连oracle库 报System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本
  4. MJExtension框架介绍
  5. Intellij IDEA 导入Eclipse或MyEclipse的Web项目
  6. GCC,GDB,Makefile和IO复用函数
  7. POJ 1930
  8. 一个资深java面试官的“面试心得”
  9. 常用CSS样式 持续更新
  10. Cousera课程Learning How to Learn学习报告
  11. javascript设计模式——组合模式
  12. YEP_footstepsounds
  13. GCC编译器原理(三)------编译原理三:编译过程(3)---编译之汇编以及静态链接【2】
  14. SELinux简介(转载)
  15. WIN10在loadrunner安装或path4插件安装时遇到的管理员阻止程序运行---解决方案(可用)
  16. VisualSVN+TortoiseSVN搭建版本控制系统教程
  17. windows 环境下如何使用virtualenv python环境管理工具
  18. HDU 3068 最长回文(manacher模板题)
  19. windows svn 客户端连不上linux svn server
  20. hdu 刷题记录

热门文章

  1. DataFactory连接MySQL数据库
  2. Android7.0 API变更
  3. 限定某个业务控制方法,只允许GET或POST请求方式访问
  4. 机器学习之Xgboost算法
  5. 阿里云轻应用服务器配置Ubuntu的JDK、Tmocat、Mysql和Redis
  6. Spring策略模式的实现
  7. ScrollView-基本设置
  8. 转:获取windows凭证管理器明文密码
  9. vue路由传参的几种基本方式
  10. Vue+element 需要用到拖拽组件 vuedraggable