一個文件移動功能

import java.io.File;

import org.apache.tools.ant.taskdefs.Mkdir;
import org.eclipse.jdt.core.dom.ThisExpression;

/**
* @Author chenrd
* @Date 2019-10-24 下午5:16:44
* @Version 1.0 业务说明:
*
*/

public class MoveFile {
public static void main(String[] agrs) {

// move("D:\zdjsqq122.dmp", "E:\myFl");

// C:UsersAdministratorDesktopchenrd茂南局
/*
* File file = new File("D:\zdjsqq11.dmp");
* System.out.println(file.isFile());
* System.out.println(file.isDirectory());
*/

// System.out.println(returnTheDirName("定值通知单【xxx】-联络开关"));

// File file = new
// File("C:\Users\Administrator\Desktop\chenrd\化州局");

// File file = new
// File("C:\Users\Administrator\Desktop\chenrd\10kV宝圩甲线\HZ2018.xlsx");
//
// if (file.isFile()) {
// String cj = returnTheDirName(file.getName());
// // System.out.println("cj=="+cj);
// String mkDir = mkDir(cj);
// // System.out.println("mkDir=="+mkDir);
// // System.out.println(file.getAbsolutePath());
// move(file.getAbsolutePath(), mkDir);
// }

  String path = "C:\Users\Administrator\Desktop\chenrd\茂南局";

handleFolderFile(path);

}

/**
* @param path
* 使用递归 传入一个目录,遍历所有,如果是目录继续遍历,如果是文件则移动
*/
public static void handleFolderFile(String path) {

File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
if (null == files || files.length == 0) {
System.out.println(path + " --> 文件夹是空的!");
} else {
for (File file2 : files) {

String fileName2 = file2.getName();
if (file2.isDirectory()) {
if (yaoMaForDir(fileName2)) {
handleFolderFile(file2.getAbsolutePath());
}
} else {
boolean yaoMa = yaoMaForFile(file2.getAbsolutePath());
if (yaoMa) {
moveFile(file2.getAbsolutePath());
}else {
//移动到未分类
System.out.println("移动到未分类");
moveFileToUnKnow(file2.getAbsolutePath());
}
}
}
}
} else {
System.out.println(path + "文件不存在!");
}
}


/**
* @param fileName
* void 转入文件 执行并移动操作
*
*/
public static void moveFile(String absolutePath) {
File file = new File(absolutePath);
if (file.isFile()) {
String cj = returnTheDirName(file.getName());
String mkDir = mkDir(cj);
move(file.getAbsolutePath(), mkDir);
}
}

/**
* @param fileName
* void 转入文件 执行并移动操作
*
*/
public static void moveFileToUnKnow(String absolutePath) {
File file = new File(absolutePath);
if (file.isFile()) {
String mkDir = mkDir("没有厂家的啊啊啊啊啊啊");
move(file.getAbsolutePath(), mkDir);
}
}

/**
* @param fileName
* @return 排除 保护配置图,单线图等文件夹
*/
public static boolean yaoMaForDir(String fileName2) {
if ( fileName2.indexOf("作废") == -1 && fileName2.indexOf("执行登记表") == -1) {
return true;
} else {

}
return false;
}

/**
* @param fileName
* @return 转入文件名,如果包含【 】 如果是返回true
*/
public static boolean yaoMaForFile(String fileName) {
if (fileName.indexOf("【") != -1 && fileName.indexOf("】") != -1) {
return true;
} else {

}
return false;
}

/**
* 返回【xxx】中的xxx
*/
public static String returnTheDirName(String fileName) {
int start = fileName.indexOf("【");
int end = fileName.indexOf("】");

fileName = fileName.substring(start + 1, end);
return fileName;
}

/**
* 创建目录,返回目录
*/
public static String mkDir(String fileName) {
String filePath = "E:" + File.separator + "myFl" + File.separator + fileName;
File file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
return filePath;
}

/**
* @param fileName
* @return 判断是否为 目录,如果是返回true
*/
public static boolean isDir(String fileName) {
File file = new File(fileName);
if (file.isDirectory()) {
return true;
}
return false;
}

/**
* @param fileName
* @return 判断是否为 文件,如果是返回true
*/
public static boolean isFile(String fileName) {
File file = new File(fileName);
if (file.isFile()) {
return true;
}
return false;
}

/**
* @param absolutePath
* @param targetDir
* 传入文件名 和目标文件夹,把文件拷贝到目标文件夹 用法示例: move("D:\zdjsqq122.dmp",
* "E:\myFl");
*
*/
public static void move(String absolutePath, String targetDir) {
try {
File file = new File(absolutePath); // 源文件
if (file.renameTo(new File(targetDir + File.separator + file.getName()))) // 源文件移动至目标文件目录
{
System.out.println("File is moved successful!");// 输出移动成功
} else {
System.out.println("File is failed to move !");// 输出移动失败
System.out.println(absolutePath);
}
// System.out.println(fileName);
} catch (Exception e) {
System.out.println("移动文件出错,文件名为:");
System.out.println(absolutePath);
e.printStackTrace();
}
}
}

原文地址:https://www.cnblogs.com/rdchen/p/11737286.html