public class FileUtils {
// customException true-抛自定义异常(方法无返回值) false-关闭自定义异常(方法有返回值)
private static boolean customException = true;
// 将符合条件的File放入集合返回
private static List<File> mList = new ArrayList<>();
/**
* 判断文件/文件夹是否存在
*
* @param Address
* 文件/文件夹地址
* @return
*/
public static boolean isExist(String Address) {
File file = new File(Address);
boolean exists = file.exists();
return exists;
}
/**
* 判断是否是文件
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @return
*/
public static boolean isFile(String fileAddress) {
File file = new File(fileAddress);
boolean isFile = file.isDirectory();
return isFile;
}
/**
* 判断是否是文件夹
*
* @param folderAddress
* 文件夹地址 : C:\\AAA\\BBB
* @return
*/
public static boolean isDirectory(String folderAddress) {
File folder = new File(folderAddress);
boolean isDirectory = folder.isDirectory();
return isDirectory;
}
/**
* 判断是否可读
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @return
*/
public static boolean canRead(String fileAddress) {
File file = new File(fileAddress);
boolean canRead = file.canRead();
return canRead;
}
/**
* 设置文件是否可读
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @param readable
* true/false 是否可读
*/
public static void setFileReadable(String fileAddress, boolean readable) {
File file = new File(fileAddress);
file.setReadable(readable);
}
/**
* 判断文件是否可写
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @return
*/
public static boolean canWrite(String fileAddress) {
File file = new File(fileAddress);
boolean canWrite = file.canWrite();
return canWrite;
}
/**
* 设置文件是否可写
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @param writable
* true/false 是否可写
*/
public static void setFileWritable(String fileAddress, boolean writable) {
File file = new File(fileAddress);
file.setWritable(writable);
}
/**
* 判断文件是否隐藏
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @return
*/
public static boolean isFileHidden(String fileAddress) {
File file = new File(fileAddress);
boolean hidden = file.isHidden();
return hidden;
}
/**
* 获取文件最后修改时间 毫秒值
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @return
*/
public static String lastModify(String fileAddress) {
File file = new File(fileAddress);
long lastModified = file.lastModified();
Date date = new Date(lastModified);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String format = sdf.format(date);
return format;
}
/**
* 创建文件
*
* @param fileDir
* 文件路径: C:\\AAA\\BBB
* @param fileName
* 文件名称: \\FILENAME
* @throws IOException
* @throws CustomException
*/
public static boolean createFile(String fileDir, String fileName)
throws IOException, CustomException {
// 如果名称为空直接抛出异常
if (fileName == null || fileName == "") {
throw new CustomException("文件名称不能为空!");
}
// 判断文件所属的文件夹是否存在,不存在则创建
boolean createFolder = createFolder(fileDir);
boolean createNewFile = true;
if (createFolder) {
File file = new File(fileDir + fileName);
// 判断文件是否存在不存在则创建
if (!file.exists()) {
createNewFile = file.createNewFile();
}
}
// 如果文件创建失败抛出异常
if (customException) {
if (!createNewFile) {
throw new CustomException("文件创建失败!");
}
}
return createNewFile;
}
/**
* 创建文件夹
*
* @param folderDir
* 文件夹地址: C:\\AAA\\BBB
* @return
* @throws CustomException
*/
public static boolean createFolder(String folderDir) throws CustomException {
File file = new File(folderDir);
boolean mkdirs = true;
// 判断文件夹是否存在,不存在则创建
if (!file.exists()) {
mkdirs = file.mkdirs();
}
// 如果文件夹创建失败抛出异常
if (customException) {
if (!mkdirs) {
throw new CustomException("文件夹创建失败!");
}
}
return mkdirs;
}
/**
* 修改文件及文件夹名称
*
* @param folderDir
* 文件夹路径 : C:\\AAA\\BBB
* @param oldName
* 原名称:\\OLDFILENAME
* @param newName
* 新名称:\\NEWFILENAME
* @return
* @throws CustomException
*/
public static boolean renameFiles(String folderDir, String oldName,
String newName) throws CustomException {
// 判断修改路径是否正确,不正确抛出异常
File folder = new File(folderDir);
if (customException) {
if (!folder.exists()) {
throw new CustomException("文件路径错误!");
}
}
File oldFile = new File(folderDir + oldName);
File newFile = new File(folderDir + newName);
boolean renameTo = oldFile.renameTo(newFile);
// 判断名称是否修改成功,失败抛出异常
if (customException) {
if (!renameTo) {
throw new CustomException("修改名称失败!");
}
}
return renameTo;
}
/**
* 删除指定(文件夹)
*
* @param folderDir
* 文件夹路径 : C:\\AAA\\BBB
* @return
* @throws CustomException
*/
public static boolean delFolder(String folderDir) throws CustomException {
// 判断待删除文件是否存在,不存在抛出异常
File file = new File(folderDir);
if (customException) {
if (!file.exists()) {
throw new CustomException("文件不存在!");
}
}
boolean delFolders = delFolders(file);
return delFolders;
}
/**
* 删除指定(文件)
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @return
* @throws CustomException
*/
public static boolean delFile(String fileAddress) throws CustomException {
// 判断待删除文件是否存在,不存在抛出异常
File file = new File(fileAddress);
if (customException) {
if (!file.exists()) {
throw new CustomException("文件不存在!");
}
}
boolean delete = file.delete();
return delete;
}
/**
* 获取文件夹下所有文件的名字
*
* @param folderDir
* 文件夹地址: C:\\AAA\\BBB
* @return
*/
public static List<String> getFolderFilenames(String folderDir) {
File folder = new File(folderDir);
String[] fileNames = folder.list();
List<String> mListFilenames = Arrays.asList(fileNames);
return mListFilenames;
}
/**
* 获取文件夹下所有文件或文件夹对象
*
* @param folderDir
* 文件夹地址: C:\\AAA\\BBB
* @return
*/
public static List<File> getFolderFiles(String folderDir) {
File folder = new File(folderDir);
File[] listFiles = folder.listFiles();
List<File> mListFiles = Arrays.asList(listFiles);
return mListFiles;
}
/**
* 获取指定文件夹下所有符合条件的文件
*
* @param folderDir
* 文件夹地址: C:\\AAA\\BBB
* @param condition
* 条件
* @return
* @throws CustomException
*/
public static List<File> getAssignFiles(String folderDir, String condition)
throws CustomException {
File folder = new File(folderDir);
if (!folder.exists()) {
throw new CustomException("文件夹路径不正确!");
}
isFiles(folder, condition);
return mList;
}
// 如果是文件夹-递归获取 如果是文件-直接存储集合
private static void isFiles(File folder, String condition) {
// 如果是文件存储集合
if (folder.isFile()) {
if (folder.getName().endsWith(condition)) {
mList.add(folder);
}
} else {// 递归文件夹获取文件
File[] files = folder.listFiles();
for (File f : files) {
isFiles(f, condition);
}
}
}
// 如果是文件夹-递归获取 如果是文件-直接删除
private static boolean delFolders(File folder) {
if (folder.isFile()) {
folder.delete();
} else {
File[] files = folder.listFiles();
for (File f : files) {
delFolders(f);
}
}
return folder.delete();
}
}
/**
* 自定义异常
*
* @author Simon
*
*/
class CustomException extends Exception {
public CustomException() {
super();
}
public CustomException(String message) {
super(message);
}
}
// customException true-抛自定义异常(方法无返回值) false-关闭自定义异常(方法有返回值)
private static boolean customException = true;
// 将符合条件的File放入集合返回
private static List<File> mList = new ArrayList<>();
/**
* 判断文件/文件夹是否存在
*
* @param Address
* 文件/文件夹地址
* @return
*/
public static boolean isExist(String Address) {
File file = new File(Address);
boolean exists = file.exists();
return exists;
}
/**
* 判断是否是文件
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @return
*/
public static boolean isFile(String fileAddress) {
File file = new File(fileAddress);
boolean isFile = file.isDirectory();
return isFile;
}
/**
* 判断是否是文件夹
*
* @param folderAddress
* 文件夹地址 : C:\\AAA\\BBB
* @return
*/
public static boolean isDirectory(String folderAddress) {
File folder = new File(folderAddress);
boolean isDirectory = folder.isDirectory();
return isDirectory;
}
/**
* 判断是否可读
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @return
*/
public static boolean canRead(String fileAddress) {
File file = new File(fileAddress);
boolean canRead = file.canRead();
return canRead;
}
/**
* 设置文件是否可读
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @param readable
* true/false 是否可读
*/
public static void setFileReadable(String fileAddress, boolean readable) {
File file = new File(fileAddress);
file.setReadable(readable);
}
/**
* 判断文件是否可写
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @return
*/
public static boolean canWrite(String fileAddress) {
File file = new File(fileAddress);
boolean canWrite = file.canWrite();
return canWrite;
}
/**
* 设置文件是否可写
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @param writable
* true/false 是否可写
*/
public static void setFileWritable(String fileAddress, boolean writable) {
File file = new File(fileAddress);
file.setWritable(writable);
}
/**
* 判断文件是否隐藏
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @return
*/
public static boolean isFileHidden(String fileAddress) {
File file = new File(fileAddress);
boolean hidden = file.isHidden();
return hidden;
}
/**
* 获取文件最后修改时间 毫秒值
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @return
*/
public static String lastModify(String fileAddress) {
File file = new File(fileAddress);
long lastModified = file.lastModified();
Date date = new Date(lastModified);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String format = sdf.format(date);
return format;
}
/**
* 创建文件
*
* @param fileDir
* 文件路径: C:\\AAA\\BBB
* @param fileName
* 文件名称: \\FILENAME
* @throws IOException
* @throws CustomException
*/
public static boolean createFile(String fileDir, String fileName)
throws IOException, CustomException {
// 如果名称为空直接抛出异常
if (fileName == null || fileName == "") {
throw new CustomException("文件名称不能为空!");
}
// 判断文件所属的文件夹是否存在,不存在则创建
boolean createFolder = createFolder(fileDir);
boolean createNewFile = true;
if (createFolder) {
File file = new File(fileDir + fileName);
// 判断文件是否存在不存在则创建
if (!file.exists()) {
createNewFile = file.createNewFile();
}
}
// 如果文件创建失败抛出异常
if (customException) {
if (!createNewFile) {
throw new CustomException("文件创建失败!");
}
}
return createNewFile;
}
/**
* 创建文件夹
*
* @param folderDir
* 文件夹地址: C:\\AAA\\BBB
* @return
* @throws CustomException
*/
public static boolean createFolder(String folderDir) throws CustomException {
File file = new File(folderDir);
boolean mkdirs = true;
// 判断文件夹是否存在,不存在则创建
if (!file.exists()) {
mkdirs = file.mkdirs();
}
// 如果文件夹创建失败抛出异常
if (customException) {
if (!mkdirs) {
throw new CustomException("文件夹创建失败!");
}
}
return mkdirs;
}
/**
* 修改文件及文件夹名称
*
* @param folderDir
* 文件夹路径 : C:\\AAA\\BBB
* @param oldName
* 原名称:\\OLDFILENAME
* @param newName
* 新名称:\\NEWFILENAME
* @return
* @throws CustomException
*/
public static boolean renameFiles(String folderDir, String oldName,
String newName) throws CustomException {
// 判断修改路径是否正确,不正确抛出异常
File folder = new File(folderDir);
if (customException) {
if (!folder.exists()) {
throw new CustomException("文件路径错误!");
}
}
File oldFile = new File(folderDir + oldName);
File newFile = new File(folderDir + newName);
boolean renameTo = oldFile.renameTo(newFile);
// 判断名称是否修改成功,失败抛出异常
if (customException) {
if (!renameTo) {
throw new CustomException("修改名称失败!");
}
}
return renameTo;
}
/**
* 删除指定(文件夹)
*
* @param folderDir
* 文件夹路径 : C:\\AAA\\BBB
* @return
* @throws CustomException
*/
public static boolean delFolder(String folderDir) throws CustomException {
// 判断待删除文件是否存在,不存在抛出异常
File file = new File(folderDir);
if (customException) {
if (!file.exists()) {
throw new CustomException("文件不存在!");
}
}
boolean delFolders = delFolders(file);
return delFolders;
}
/**
* 删除指定(文件)
*
* @param fileAddress
* 文件地址 : C:\\AAA\\BBB\\FILENAME
* @return
* @throws CustomException
*/
public static boolean delFile(String fileAddress) throws CustomException {
// 判断待删除文件是否存在,不存在抛出异常
File file = new File(fileAddress);
if (customException) {
if (!file.exists()) {
throw new CustomException("文件不存在!");
}
}
boolean delete = file.delete();
return delete;
}
/**
* 获取文件夹下所有文件的名字
*
* @param folderDir
* 文件夹地址: C:\\AAA\\BBB
* @return
*/
public static List<String> getFolderFilenames(String folderDir) {
File folder = new File(folderDir);
String[] fileNames = folder.list();
List<String> mListFilenames = Arrays.asList(fileNames);
return mListFilenames;
}
/**
* 获取文件夹下所有文件或文件夹对象
*
* @param folderDir
* 文件夹地址: C:\\AAA\\BBB
* @return
*/
public static List<File> getFolderFiles(String folderDir) {
File folder = new File(folderDir);
File[] listFiles = folder.listFiles();
List<File> mListFiles = Arrays.asList(listFiles);
return mListFiles;
}
/**
* 获取指定文件夹下所有符合条件的文件
*
* @param folderDir
* 文件夹地址: C:\\AAA\\BBB
* @param condition
* 条件
* @return
* @throws CustomException
*/
public static List<File> getAssignFiles(String folderDir, String condition)
throws CustomException {
File folder = new File(folderDir);
if (!folder.exists()) {
throw new CustomException("文件夹路径不正确!");
}
isFiles(folder, condition);
return mList;
}
// 如果是文件夹-递归获取 如果是文件-直接存储集合
private static void isFiles(File folder, String condition) {
// 如果是文件存储集合
if (folder.isFile()) {
if (folder.getName().endsWith(condition)) {
mList.add(folder);
}
} else {// 递归文件夹获取文件
File[] files = folder.listFiles();
for (File f : files) {
isFiles(f, condition);
}
}
}
// 如果是文件夹-递归获取 如果是文件-直接删除
private static boolean delFolders(File folder) {
if (folder.isFile()) {
folder.delete();
} else {
File[] files = folder.listFiles();
for (File f : files) {
delFolders(f);
}
}
return folder.delete();
}
}
/**
* 自定义异常
*
* @author Simon
*
*/
class CustomException extends Exception {
public CustomException() {
super();
}
public CustomException(String message) {
super(message);
}
}