
com.moon.core.io.FileUtil Maven / Gradle / Ivy
package com.moon.core.io;
import com.moon.core.lang.LangUtil;
import com.moon.core.lang.StringUtil;
import com.moon.core.lang.ThrowUtil;
import com.moon.core.util.IteratorUtil;
import com.moon.core.util.function.ThrowingConsumer;
import java.io.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import static com.moon.core.lang.LangUtil.applyBi;
import static com.moon.core.lang.ThrowUtil.runtime;
/**
* @author moonsky
*/
public final class FileUtil {
private FileUtil() { ThrowUtil.noInstanceError(); }
/*
* -----------------------------------------------------------------------
* copies
* -----------------------------------------------------------------------
*/
/**
* 将 sourceFilepath 绝对路径所指向的文件或目录复制到 targetDir
*
* @param sourceFilepath 源路径
* @param targetDir 目标目录
*/
public static void copyToDirectory(String sourceFilepath, String targetDir) {
copyToDirectory(new File(sourceFilepath), new File(targetDir));
}
/**
* 将 sourceFilepath 绝对路径所指向的文件复制到 targetDir,并将文件命名为 newFileName
* 新文件名设置只针对复制文件有效,如果是复制文件夹,将使用原文件名
*
* @param sourceFilepath 源路径
* @param targetDir 目标目录
*/
public static void copyToDirectory(String sourceFilepath, String targetDir, String newFileName) {
copyToFile(new File(sourceFilepath), new File(targetDir, newFileName));
}
public static void copyToDirectory(File sourceFile, final File targetDir) {
if (sourceFile == null || targetDir == null) {
return;
} else if (sourceFile.isFile()) {
copyToFile(sourceFile, new File(targetDir, sourceFile.getName()));
} else if (sourceFile.isDirectory()) {
String root = sourceFile.getParent();
int len = root.length();
IteratorUtil.forEach(traverseDirectory(sourceFile),
file -> copyToFile(file, new File(targetDir, file.getAbsolutePath().substring(len))));
}
}
public static void copyToFile(String sourceFilepath, String targetFilePath) {
copyToFile(new File(sourceFilepath), new File(targetFilePath));
}
public static void copyToFile(File sourceFile, File targetFile) {
if (exists(sourceFile)) {
if (sourceFile.isDirectory()) {
copyToDirectory(sourceFile, targetFile.getParentFile());
} else if (sourceFile.isFile()) {
try (FileOutputStream output = getOutputStream(targetFile);
FileInputStream input = getInputStream(sourceFile)) {
IOUtil.copy(input, output);
} catch (IOException e) {
runtime(e);
}
}
}
}
/*
* -----------------------------------------------------------------------
* io
* -----------------------------------------------------------------------
*/
/**
* 获取文件的输出流,如果文件不存在,会创建文件以及目录结构,创建失败返回空
*
* @param file 目标文件
*/
public static FileOutputStream getOutputStream(File file) { return getOutputStream(file, false); }
public static FileOutputStream getOutputStream(String filePath) { return getOutputStream(filePath, false); }
public static FileOutputStream getOutputStream(File file, boolean append) {
return createNewFile(file) ? applyBi(file,
append,
FileOutputStream::new) : ThrowUtil.runtime("File not exist: " + file);
}
public static FileOutputStream getOutputStream(String filePath, boolean append) {
return getOutputStream(new File(StringUtil.trimToEmpty(filePath)), append);
}
public static FileInputStream getInputStream(String filePath) {
return getInputStream(new File(StringUtil.trimToEmpty(filePath)));
}
/**
* 从已知文件获取输入流,如不存在返回空
*
* @param file 目标文件
*/
public static FileInputStream getInputStream(File file) { return LangUtil.apply(file, FileInputStream::new); }
public static File getFile(String... paths) {
int length = paths == null ? 0 : paths.length;
if (length < 1) {
return null;
}
if (length == 1) {
return new File(paths[0]);
}
File file = new File(paths[0], paths[1]);
for (int i = 2; i < length; i++) {
file = new File(file, paths[i]);
}
return file;
}
/*
* -----------------------------------------------------------------------
* creates
* -----------------------------------------------------------------------
*/
public static boolean mkdirs(String... pathOptions) {
File dir = getFile(pathOptions);
return dir != null && mkdirs(dir);
}
public static boolean mkdirs(String path) { return mkdirs(new File(path)); }
public static boolean mkdirs(File dir) {
if (dir.exists()) {
if (dir.isFile()) {
return ThrowUtil.runtime("The target exist and is a file: " + dir);
} else {
return true;
}
} else {
return dir.mkdirs();
}
}
/**
* 创建文件以其目录结构,返回创建成功与否的状态
*
* @param file 目标文件
*/
public static boolean createNewFile(File file) {
if (!exists(file)) {
File parent = file.getParentFile();
if (parent.exists() || parent.mkdirs()) {
try {
return file.createNewFile();
} catch (IOException e) {
return false;
}
}
}
return true;
}
public static boolean createNewFile(String filepath) { return createNewFile(new File(filepath)); }
/*
* -----------------------------------------------------------------------
* travellers
* -----------------------------------------------------------------------
*/
/**
* 文件列表遍历器
*
* @return 文件列表遍历器
*/
public static FileTraveller traveller() { return new FileTraveller(); }
/**
* 遍历指定目录的文件列表,如遇不可访问的安全保护会打印相应错误信息,但不会影响程序执行
*
* @param dirPath 目标文件路径
*/
public static List traverseDirectory(String dirPath) { return traveller().traverse(dirPath); }
public static List traverseDirectory(File dirPath) { return traveller().traverse(dirPath); }
public static List traverse(String dirPath) { return traveller().traverse(dirPath); }
public static List traverse(File dir) { return traveller().traverse(dir); }
public static List traverseAll(File... dirs) {
FileTraveller traveller = traveller();
for (int i = 0; i < dirs.length; i++) {
traveller.traverse(dirs[i]);
}
return traveller;
}
public static List traverseAll(String... dirs) {
Traveller traveller = traveller();
for (int i = 0; i < dirs.length; i++) {
traveller.traverse(dirs[i]);
}
return traveller;
}
public static List traverseAll(List dirs) {
FileTraveller traveller = traveller();
for (int i = 0; i < dirs.size(); i++) {
traveller.traverse(dirs.get(i));
}
return traveller;
}
/*
* -----------------------------------------------------------------------
* write or append lines
* append:追加字符串行至文件末尾
* write: 可手动控制是追加还是覆盖
* -----------------------------------------------------------------------
*/
private static ThrowingConsumer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy