package top.feb13th.deploy.util;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.StringJoiner;
/**
* 输入输出工具类
*
* @author zhoutaotao
* @date 2019/9/24 16:26
*/
public class IoUtil {
/**
* 获取系统临时目录
*
* @param dirs 在临时目录后额外增加的文件名称
* @return 系统临时目录
*/
public static String getTmpdir(String... dirs) {
String tmpdir = System.getProperty("java.io.tmpdir");
StringJoiner joiner = new StringJoiner(File.separator);
joiner.add(tmpdir);
for (String dir : dirs) {
joiner.add(dir);
}
return joiner.toString();
}
/**
* 获取应用程序的运行路径
*
* @param dirs 子目录
* @return 应用程序运行目录 + 子目录
*/
public static String getAppDir(String... dirs) {
// 获取当前程序的运行路径
String location = IoUtil.class.getProtectionDomain().getCodeSource().getLocation()
.getFile();
if (location.startsWith("file:")) {
location = location.substring(5);
}
if (location.contains(".jar")) {
location = location.substring(0, location.indexOf(".jar"));
File file = new File(location);
location = file.getParent();
}
File file = new File(location);
if (file.isFile()) {
// 如果当前是执行的jar文件, 获取jar文件的所在目录
location = file.getParent();
}
return appendPath(location, dirs);
}
/**
* 拼接路径
*
* @param prefix 前缀
* @param paths 路径列表
* @return 路径
*/
public static String appendPath(String prefix, String... paths) {
if (StringUtil.isBlank(prefix)) {
return null;
}
StringJoiner joiner = new StringJoiner(File.separator);
joiner.add(prefix);
for (String path : paths) {
joiner.add(path);
}
return joiner.toString();
}
/**
* 删除文件
*
* @param dirPath 文件路径
*/
public static void delete(String dirPath) {
// 文件路径为空时, 直接返回
if (StringUtil.isBlank(dirPath)) {
return;
}
File dirFile = new File(dirPath);
String[] list = dirFile.list();
if (list != null && list.length > 0) {
for (String filename : list) {
String newFilePath = dirPath + File.separator + filename;
File file = new File(newFilePath);
if (file.isDirectory()) {
delete(newFilePath);
}
file.delete();
}
}
if (dirFile.exists()) {
dirFile.delete();
}
}
/**
* 目录内容拷贝,只拷贝文件内部的文件及文件夹
*
* @param from 源目录
* @param to 目标目录
*/
public static void copyDir(File from, File to) {
String[] list;
if (!from.isDirectory() || (list = from.list()) == null) {
return;
}
String fromPath = from.getAbsolutePath();
String toPath = to.getAbsolutePath();
for (String filename : list) {
String fromFilepath = fromPath + File.separator + filename;
String toFilepath = toPath + File.separator + filename;
File fromFile = new File(fromFilepath);
File toFile = new File(toFilepath);
if (fromFile.isDirectory()) {
// 如果赋值的是文件夹,则创建该文件夹, 并进入文件夹内部拷贝文件
toFile.mkdirs();
copyDir(fromFile, toFile);
} else {
// 拷贝文件内容
copyFile(fromFile, toFile);
}
}
}
/**
* 拷贝两个文件的内容
*
* @param from 源文件
* @param to 目标文件
*/
public static void copyFile(File from, File to) {
FileInputStream fromIs = null;
FileOutputStream toOs = null;
FileChannel fromChannel = null;
FileChannel toChannel = null;
try {
if (!to.exists()) {
createFile(to);
}
fromIs = new FileInputStream(from);
toOs = new FileOutputStream(to);
fromChannel = fromIs.getChannel();
toChannel = toOs.getChannel();
fromChannel.transferTo(0, fromChannel.size(), toChannel);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
close(fromChannel);
close(fromIs);
close(toChannel);
close(toOs);
}
}
public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
/**
* 从指定文件中读取字符串
*
* @param filepath 文件路径
* @return 如果文件不存在则返回null, 否则返回文件字符串内容
*/
public static String readString(String filepath) {
Path path = Paths.get(new File(filepath).toURI());
if (!Files.exists(path)) {
return null;
}
try {
return Files.readAllLines(path, StandardCharsets.UTF_8).stream().reduce("", String::concat);
} catch (IOException e) {
return null;
}
}
/**
* 向文件中写入content
*
* @param filepath 文件路径
* @param content 文件内容字符串
*/
public static void writeString(String filepath, String... content) {
try {
Path path = Paths.get(new File(filepath).toURI());
Files.deleteIfExists(path);
Path parent = path.getParent();
if (!Files.exists(parent)) {
Files.createDirectories(parent);
}
Files.createFile(path);
Files.write(path, Arrays.asList(content), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 创建新文件, 如果存在则删除重新创建
*
* @param filepath 文件路径
*/
public static void createFile(String filepath) {
if (StringUtil.isBlank(filepath)) {
throw new NullPointerException("filepath must not empty");
}
createFile(new File(filepath));
}
/**
* 创建新文件, 如果存在则删除重新创建
*
* @param file 文件
*/
public static void createFile(File file) {
if (ObjectUtil.isNull(file)) {
throw new NullPointerException("core must not null");
}
try {
Path path = Paths.get(file.toURI());
Files.deleteIfExists(path);
Path parent = path.getParent();
if (!Files.exists(parent)) {
Files.createDirectories(parent);
}
Files.createFile(path);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 创建目录
*
* @param directorPath 目录路径
*/
public static void createDirector(String directorPath) {
if (StringUtil.isBlank(directorPath)) {
throw new NullPointerException("director path must not empty");
}
createDirector(new File(directorPath));
}
/**
* 创建目录
*
* @param file 目录
*/
public static void createDirector(File file) {
if (ObjectUtil.isNull(file)) {
throw new NullPointerException("director core must not null");
}
try {
Path path = Paths.get(file.toURI());
if (!Files.exists(path)) {
Files.createDirectories(path);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}