forked from fanruan/finekit
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
220 lines
4.8 KiB
220 lines
4.8 KiB
6 years ago
|
package com.fanruan.api.runtime;
|
||
|
|
||
|
import com.fr.io.utils.ResourceIOUtils;
|
||
|
import com.fr.stable.Filter;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStream;
|
||
|
|
||
|
/**
|
||
|
* @author richie
|
||
|
* @version 10.0
|
||
|
* Created by richie on 2019-09-19
|
||
|
* 操作运行环境下文件的工具方法
|
||
|
*/
|
||
|
public class ResourceKit {
|
||
|
|
||
|
/**
|
||
|
* 路径是否存在
|
||
|
*
|
||
|
* @param target 路径
|
||
|
* @return 是否存在
|
||
|
*/
|
||
|
public static boolean exist(String target) {
|
||
|
return ResourceIOUtils.exist(target);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 创建文件
|
||
|
*
|
||
|
* @param file 文件路径
|
||
|
* @return 是否存在
|
||
|
*/
|
||
|
public static boolean createFile(String file) {
|
||
|
return ResourceIOUtils.createFile(file);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 创建文件夹
|
||
|
*
|
||
|
* @param dir 文件夹路径
|
||
|
* @return 是否成功
|
||
|
*/
|
||
|
public static boolean createDirectory(String dir) {
|
||
|
return ResourceIOUtils.createDirectory(dir);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 是否为文件夹
|
||
|
*
|
||
|
* @param path 路径
|
||
|
* @return 是否是文件夹
|
||
|
*/
|
||
|
public static boolean isDirectory(String path) {
|
||
|
return ResourceIOUtils.isDirectory(path);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取文件或者文件夹的名称
|
||
|
*
|
||
|
* @param path 路径
|
||
|
* @return 名称
|
||
|
*/
|
||
|
public static String getName(String path) {
|
||
|
return ResourceIOUtils.getName(path);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 列出路径下的所有内容的名称
|
||
|
*
|
||
|
* @param dir 路径
|
||
|
* @return 所有内容名称List
|
||
|
*/
|
||
|
public static String[] list(String dir) {
|
||
|
return ResourceIOUtils.list(dir);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 列出路径下的所有符合要求的内容的名称
|
||
|
*
|
||
|
* @param dir 路径
|
||
|
* @param filter 过滤器
|
||
|
* @return 所有内容名称List
|
||
|
*/
|
||
|
public static String[] list(String dir, Filter<String> filter) {
|
||
|
return ResourceIOUtils.list(dir, filter);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 文件夹是否存在
|
||
|
*
|
||
|
* @param dir 文件夹
|
||
|
* @return 是否存在
|
||
|
*/
|
||
|
public static boolean isDirectoryExist(String dir) {
|
||
|
return ResourceIOUtils.isDirectoryExist(dir);
|
||
|
}
|
||
|
|
||
|
|
||
|
public static String getRealPath(String relativePath) {
|
||
|
return ResourceIOUtils.getRealPath(relativePath);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 读取文件到流
|
||
|
* <p>
|
||
|
* 相对路径下如果是仓库资源从仓库读取,其它从本地读取
|
||
|
* <p>
|
||
|
* 据对路径按绝对路径读
|
||
|
*
|
||
|
* @param path 路径
|
||
|
* @return 流
|
||
|
*/
|
||
|
public static InputStream read(String path) {
|
||
|
return ResourceIOUtils.read(path);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 读取文件到bytes
|
||
|
*
|
||
|
* @param path 文件路径
|
||
|
* @return bytes
|
||
|
*/
|
||
|
public static byte[] readBytes(String path) {
|
||
|
return ResourceIOUtils.readBytes(path);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 写入数据流到文件
|
||
|
*
|
||
|
* @param path 文件路径
|
||
|
* @param data 数据
|
||
|
*/
|
||
|
public static void write(String path, InputStream data) {
|
||
|
ResourceIOUtils.write(path, data);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 写入字符串数据到文件
|
||
|
*
|
||
|
* @param path 文件路径
|
||
|
* @param data 数据
|
||
|
*/
|
||
|
public static void write(String path, String data) {
|
||
|
ResourceIOUtils.write(path, data);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 写入字节数组数据到文件
|
||
|
*
|
||
|
* @param path 文件路径
|
||
|
* @param data 数据
|
||
|
*/
|
||
|
public static void write(String path, byte[] data) {
|
||
|
ResourceIOUtils.write(path, data);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取父路径
|
||
|
*
|
||
|
* @param path 路径
|
||
|
* @return 父路径
|
||
|
*/
|
||
|
public static String getParent(String path) {
|
||
|
return ResourceIOUtils.getParent(path);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 从仓库删除
|
||
|
*
|
||
|
* @param path 路径
|
||
|
* @return 是否删除成功
|
||
|
*/
|
||
|
public static boolean delete(String path) {
|
||
|
return ResourceIOUtils.delete(path);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 文件的最后修改时间
|
||
|
*
|
||
|
* @param path 文件路径
|
||
|
* @return 修改的时间戳
|
||
|
*/
|
||
|
public static long lastModified(String path) {
|
||
|
return ResourceIOUtils.lastModified(path);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 文件大小
|
||
|
*
|
||
|
* @param path 文件路径
|
||
|
* @return 文件字节数
|
||
|
*/
|
||
|
public static long getLength(String path) {
|
||
|
return ResourceIOUtils.getLength(path);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 重命名
|
||
|
* <p>
|
||
|
* 文件位置根据oldFile来确定
|
||
|
*
|
||
|
* @param oldFile 原文件path
|
||
|
* @param newFile 新文件path
|
||
|
* @return 是否成功
|
||
|
*/
|
||
|
public static boolean renameTo(String oldFile, String newFile) {
|
||
|
return ResourceIOUtils.renameTo(oldFile, newFile);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 拷贝文件到某目录下
|
||
|
*
|
||
|
* @param src 源文件
|
||
|
* @param dir 目标文件
|
||
|
*/
|
||
|
public static void copy(String src, String dir) throws IOException {
|
||
|
ResourceIOUtils.copy(src, dir);
|
||
|
}
|
||
|
}
|