import lombok.extern.slf4j.Slf4j;
import me.xueyao.RandomUtils;
import me.xueyao.validate.Validators;
import me.xueyao.date.DateTools;
import java.io.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Slf4j
public class FileUtil {
private static final String FOLDER_SEPARATOR = "/";
private static final char EXTENSION_SEPARATOR = '.';
public static boolean creatFolder(String folderName) {
File folder = new File(folderName);
boolean result = false;
if (!folder.exists()) {
result = folder.mkdirs();
if (result) {
} else {
}
} else {
result = true;
}
return result;
}
public static boolean createFile(String folderName, String fileName) throws IOException {
boolean result = false;
if (creatFolder(folderName)) {
File file = new File(folderName + File.separator + fileName);
if (!file.exists()) {
try {
result = file.createNewFile();
} catch (IOException e) {
}
} else {
}
} else {
}
return result;
}
public static boolean renameFile(String folderName, String oldFileName, String newFileName) {
boolean result = false;
File file = new File(folderName + File.separator + oldFileName);
if (file.exists()) {
File newFile = new File(folderName + File.separator + newFileName);
result = file.renameTo(newFile);
} else {
}
return result;
}
public static boolean deleteFile(String folderName, String fileName) {
boolean result = false;
File file = new File(folderName + File.separator + fileName);
if (file.exists()) {
result = file.delete();
} else {
}
return result;
}
public static String getDir(String name) {
if (name != null) {
int code = name.hashCode();
return "/" + (code & 15) + "/" + (code >>> 4 & 15);
}
return null;
}
public static List<File> getNestedFiles(String path) {
File directory = new File(path);
if (!directory.exists() || !directory.isDirectory()) {
throw new IllegalArgumentException("Nonexistent directory[" + path + "]");
}
return new Recursiver().getFileList(directory);
}
private static class Recursiver {
private static List<File> files = new ArrayList<>();
public List<File> getFileList(File file) {
File[] children = file.listFiles();
for (int i = 0; i < children.length; i++) {
if (children[i].isDirectory()) {
new Recursiver().getFileList(children[i]);
} else {
files.add(children[i]);
}
}
return files;
}
}
public static void printDir(File file, int level) {
for (int i = 0; i < level; i++) {
System.out.print("\t");
}
System.out.println(file.getName());
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
printDir(f, level + 1);
}
}
}
public static void getFileType(File f, Map<String, Integer> map) {
if (f.isDirectory()) {
File[] files = f.listFiles();
for (File file : files) {
getFileType(file, map);
}
} else {
String fileName = f.getName();
String key = fileName.substring(fileName.lastIndexOf(".") + 1);
if (map.containsKey(key)) {
map.put(key, map.get(key) + 1);
} else {
map.put(key, 1);
}
}
}
public static void delete(File dir) {
File[] files = dir.listFiles();
for (File file : files) {
if (file.isFile()) {
boolean success = file.delete();
if (success) {
System.out.println(file + "删除成功");
} else {
System.out.println(file + "正在使用,删除失败");
}
} else {
delete(file);
}
}
boolean success = dir.delete();
if (success) {
System.out.println(dir + "删除成功");
} else {
System.out.println(dir + "正在使用,删除失败");
}
}
public static void filterFile(File f, final Integer size) {
if (f.isFile()) {
System.out.println(f + "不是文件夹");
return;
}
if (!f.exists()) {
return;
}
File[] files = f.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
return true;
}
if (pathname.isHidden()) {
return false;
}
long length = pathname.length();
if (length / 1024 < size) {
return true;
}
return false;
}
});
for (File file : files) {
if (file.isDirectory()) {
filterFile(file, size);
continue;
}
System.out.println(file);
}
}
public static long folderSize(File dir) {
long length = 0;
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
length += folderSize(file);
} else {
length += file.length();
}
}
return length;
}
public static String getFileMD5(File file) {
if (!file.exists() || !file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte[] buffer = new byte[1024];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
BigInteger bigInt = new BigInteger(1, digest.digest());
return bigInt.toString(16);
}
public static String getFileSuffix(String file) {
if (file == null) {
return null;
}
int extIndex = file.lastIndexOf(EXTENSION_SEPARATOR);
if (extIndex == -1) {
return null;
}
int folderIndex = file.lastIndexOf(FOLDER_SEPARATOR);
if (folderIndex > extIndex) {
return null;
}
return file.substring(extIndex + 1);
}
public static String getExtension(String fileName) {
if (Validators.isEmpty(fileName)) {
return null;
}
int pointIndex = fileName.lastIndexOf(".");
return pointIndex > 0 && pointIndex < fileName.length() ? fileName.substring(pointIndex + 1).toLowerCase()
: null;
}
public static boolean isExist(String filePath, boolean isNew) {
File file = new File(filePath);
if (!file.exists() && isNew) {
return file.mkdirs();
}
return false;
}
public static String getFileName(String type) {
return getFileName(type, "", "");
}
public static String getFileName(String type, String prefix, String suffix) {
String date = DateTools.getCurrentTime("yyyyMMddHH24mmss");
String random = RandomUtils.generateNumberString(10);
return prefix + date + random + suffix + "." + type;
}
public static String getFileName() {
String date = DateTools.getCurrentTime("yyyyMMddHH24mmss");
String random = RandomUtils.generateNumberString(10);
return date + random;
}
public void deleteAll(String dirpath) {
File path = new File(dirpath);
try {
if (!path.exists()) {
return;
}
if (path.isFile())
{
path.delete();
return;
}
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
deleteAll(files[i].getAbsolutePath());
}
path.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copy(File inputFile, File outputFile, boolean isOverWrite)
throws IOException {
if (!inputFile.exists()) {
throw new RuntimeException(inputFile.getPath() + "源目录不存在!");
}
copyPri(inputFile, outputFile, isOverWrite);
}
private static void copyPri(File inputFile, File outputFile, boolean isOverWrite) throws IOException {
if (inputFile.isFile()) {
copySimpleFile(inputFile, outputFile, isOverWrite);
} else {
if (!outputFile.exists()) {
outputFile.mkdirs();
}
for (File child : inputFile.listFiles()) {
copy(child, new File(outputFile.getPath() + "/" + child.getName()), isOverWrite);
}
}
}
private static void copySimpleFile(File inputFile, File outputFile,
boolean isOverWrite) throws IOException {
if (outputFile.exists()) {
if (isOverWrite) {
if (!outputFile.delete()) {
throw new RuntimeException(outputFile.getPath() + "无法覆盖!");
}
} else {
return;
}
}
InputStream in = new FileInputStream(inputFile);
OutputStream out = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
out.close();
}
public boolean renameDir(String oldPath, String newPath) {
File oldFile = new File(oldPath);
File newFile = new File(newPath);
return oldFile.renameTo(newFile);
}
@SuppressWarnings("resource")
public static long getFileSize(File file) throws Exception {
long size = 0;
if (file.exists()) {
FileInputStream fis;
fis = new FileInputStream(file);
size = fis.available();
} else {
file.createNewFile();
}
return size;
}
}