public class UFileUtil extends Object
Modifier and Type | Field and Description |
---|---|
static String |
CLASS_EXT
Class文件扩展名
|
static String |
JAR_FILE_EXT
Jar文件扩展名
|
static String |
JAR_PATH_EXT
在Jar中的路径jar的扩展名形式
|
static String |
PATH_FILE_PRE
当Path为文件形式时, path会加入一个表示文件的前缀
|
Constructor and Description |
---|
UFileUtil() |
Modifier and Type | Method and Description |
---|---|
static File |
file(String path)
创建File对象,自动识别相对或绝对路径,相对路径将自动从ClassPath下寻找
|
static String |
getAbsolutePath(String path)
获取绝对路径,相对于ClassPath的目录
如果给定就是绝对路径,则返回原路径,原路径把所有\替换为/ 兼容Spring风格的路径表示,例如:classpath:config/example.setting也会被识别后转换 |
static String |
getAbsolutePath(String path,
Class<?> baseClass)
获取绝对路径
此方法不会判定给定路径是否有效(文件或目录存在) |
static boolean |
isAbsolutePath(String path)
给定路径已经是绝对路径
此方法并没有针对路径做标准化,建议先执行 normalize(String) 方法标准化路径后判断 |
static boolean |
isWindows()
是否为Windows环境
|
static File |
mkParentDirs(File file)
创建所给文件或目录的父目录
|
static String |
normalize(String path)
修复路径
如果原路径尾部有分隔符,则保留为标准分隔符(/),否则不保留 1. |
static String |
readableFileSize(long size)
可读的文件大小
参考 http://stackoverflow.com/questions/3263892/format-file-size-as-mb-gb-etc |
static byte[] |
readBytes(File file)
读取文件所有数据
文件的长度不能超过Integer.MAX_VALUE |
static String |
readString(File file,
Charset charset)
读取文件内容
|
static File |
touch(File file)
创建文件及其父目录,如果这个文件存在,直接返回这个文件
此方法不对File对象类型做判断,如果File不存在,无法判断其类型 |
static File |
writeBytes(byte[] data,
File dest)
写数据到文件中
|
static File |
writeBytes(byte[] data,
File dest,
int off,
int len,
boolean isAppend)
写入数据到文件
|
static File |
writeString(String content,
File file,
Charset charset)
将String写入文件,覆盖模式
|
public static final String CLASS_EXT
public static final String JAR_FILE_EXT
public static final String JAR_PATH_EXT
public static final String PATH_FILE_PRE
public static boolean isWindows()
public static File file(String path)
path
- 文件路径public static String getAbsolutePath(String path)
path
- 相对路径public static String getAbsolutePath(String path, Class<?> baseClass)
path
- 相对路径baseClass
- 相对路径所相对的类public static boolean isAbsolutePath(String path)
normalize(String)
方法标准化路径后判断path
- 需要检查的Pathpublic static String normalize(String path)
栗子:
"/foo//" =》 "/foo/" "/foo/./" =》 "/foo/" "/foo/../bar" =》 "/bar" "/foo/../bar/" =》 "/bar/" "/foo/../bar/../baz" =》 "/baz" "/../" =》 "/" "foo/bar/.." =》 "foo" "foo/../bar" =》 "bar" "foo/../../bar" =》 "bar" "//server/foo/../bar" =》 "/server/bar" "//server/../bar" =》 "/bar" "C:\\foo\\..\\bar" =》 "C:/bar" "C:\\..\\bar" =》 "C:/bar" "~/foo/../bar/" =》 "~/bar/" "~/../bar" =》 "bar"
path
- 原路径public static String readString(File file, Charset charset) throws IORuntimeException
file
- 文件charset
- 字符集IORuntimeException
- IO异常public static File writeString(String content, File file, Charset charset) throws IORuntimeException
content
- 写入的内容file
- 文件charset
- 字符集IORuntimeException
- IO异常public static File touch(File file) throws IORuntimeException
file
- 文件对象IORuntimeException
- IO异常public static File mkParentDirs(File file)
file
- 文件或目录public static String readableFileSize(long size)
size
- Long类型大小public static byte[] readBytes(File file) throws IORuntimeException
file
- 文件IORuntimeException
- IO异常public static File writeBytes(byte[] data, File dest) throws IORuntimeException
dest
- 目标文件data
- 数据IORuntimeException
- IO异常public static File writeBytes(byte[] data, File dest, int off, int len, boolean isAppend) throws IORuntimeException
data
- 数据dest
- 目标文件off
- 数据开始位置len
- 数据长度isAppend
- 是否追加模式IORuntimeException
- IO异常Copyright © 2020. All rights reserved.