1.文件操作测试
package com.mereking.main;
import org.apache.commons.lang.StringUtils;
import com.mereking.util.FileUtils;
/**
* 文件操作测试
* @author mereking
*/
public class FileOpTest {
public static void main(String args[]) {
// 1.移动文件
String oriPath = "原文件夹\\测试文件.txt ";
String basePath = "D:\\测试文件\\";
String secPath = "移动后的文件夹\\";
String ext = ".txt";
String fileUlr = FileUtils.moveFile(oriPath, basePath, secPath, ext);
if (StringUtils.isNotBlank(fileUlr)) {
System.out.println("文件移动成功");
} else {
System.out.println("文件移动失败");
}
// 2.删除文件/删除文件夹
String oriPathDelete = "删除用文件夹\\";
String basePathDelete = "D:\\测试文件\\";
boolean result = FileUtils.deleteFolder(basePathDelete+oriPathDelete);
if (result) {
System.out.println("文件夹/文件删除成功");
} else {
System.out.println("文件夹/文件删除失败");
}
}
}
2.文件操作工具
方法目录:
- 获取唯一的文件名
- 移动文件,并给该文件定义唯一一个文件名
- 删除文件夹
package com.mereking.util;
import java.io.File;
import java.util.Date;
import org.apache.commons.lang.RandomStringUtils;
public class FileUtils {
/**
* 方法目录
* 1.获取唯一的文件名
* 2.移动文件,并给该文件定义唯一一个文件名
* 3.删除文件夹
*/
/**
* 获取唯一的文件名
* @param ext 文件扩展名
* @return
*/
public static String getUniqueFilename(String ext) {
// 当前日期的ms数+62个字母和数字,含大小写随机选择四位+扩展名构建唯一文件名
return new Date().getTime() + RandomStringUtils.random(4, Num62.N62_CHARS) + "." + ext;
// N62_CHARS具体内容如下:(可以打开其他相关代码的Num62类 查看其它具体内容)
/*
//62个字母和数字,含大小写
public static final char[] N62_CHARS = { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z' };
*/
}
/**
* 移动文件,并给该文件定义唯一一个文件名
* @param oriPath 原文件位置
* @param basePath 项目路径,为基础路径
* @param secPath 第二段移动的目标文件路径 "某某/某某/"
* @param ext 文件扩展名
* @return 返回移动后文件路径
*/
public static String moveFile(String oriPath,String basePath, String secPath, String ext) {
File file = new File(basePath + oriPath);
if(file.isFile()) {
// 获取唯一文件名(可有效解决文件移动后冲突问题)
String newFilName = getUniqueFilename(ext);
String newUrl = basePath + secPath + newFilName;
File tempFile = new File(newUrl);
// 判断该文件夹是否存在
if (!tempFile.getParentFile().exists()) {
// 文件夹不存在则建立该文件夹
tempFile.getParentFile().mkdirs();
}
// 移动文件
file.renameTo(tempFile);
return secPath + newFilName;
} else {
System.out.println("文件不存在:"+basePath + oriPath);
}
return null;
}
/**
* 删除文件夹
* @param filePath 文件夹位置
* @return 返回删除是否成功
*/
public static boolean deleteFolder(String filePath) {
boolean result = false;
File file = new File(filePath);
if(file.exists()) {
if(file.isDirectory()) {
File[] files = file.listFiles();
// 删除目录下的所有文件
for(int i = 0;i < files.length;i ++) {
if (files[i].isDirectory()) {
// 若为文件夹,则递归处理内部文件
result = deleteFolder(files[i].getAbsolutePath());
} else {
result = files[i].delete();
}
// 若中间出现错误,则停止删除
if (!result) return result;
}
result = file.delete();
} else {
result = file.delete();
}
} else {
System.out.println("文件/文件夹不存在:"+filePath);
}
return result;
}
}