Package com.imyeyu.io
Class IO
java.lang.Object
com.imyeyu.io.IO
- All Implemented Interfaces:
com.imyeyu.utils.OS.FileSystem
文件和数据流操作
- Version:
- 2021-02-13 10:15
- Author:
- 夜雨
-
Nested Class Summary
Nested Classes -
Field Summary
Fields inherited from interface com.imyeyu.utils.OS.FileSystem
COMPARATOR_FILE_NAME, SEP -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]bytesClone(byte[] bytes) 字节数组克隆static long计算文件或文件夹大小static voidstatic voidcopy(File from, String toPath, IO.OnWriteCallback callback) 复制文件static void删除文件或文件夹,删除失败不响应static File使用该路径创建文件夹static File获取文件对象,如果不存在则创建该文件static File获取文件对象,如果不存在则创建该文件static File获取文件对象,如果不存在则创建该文件static StringfileExtension(File file) 获取文件扩展名static String适应路径,在不确定路径结尾是否存在分隔符时总是补充分隔符static InputStreamgetInputStream(File file) 获取文件数据流static InputStreamgetInputStream(String path) 获取文件数据流static StringgetJarAbsolutePath(Class<?> clazz) 运行程序(.jar)所在磁盘的绝对路径,在 debug 环境中为类路径static OutputStreamgetOutputStream(File file) 获取文件的输出流(如果不存在将会创建文件)static OutputStreamgetOutputStream(String path) 获取文件的输出流(如果不存在将会创建文件)static RandomAccessFilegetRandomAccessFile(File file) 获取文件随机访问对象static RandomAccessFilegetRandomAccessFile(String path) 获取文件随机访问对象static void隐藏文件深度遍历文件夹获取所有文件static String计算文件 MD5static Stringmd5(InputStream stream) 计算输入流 MD5static void移动文件static File重命名文件static booleanresourceExist(Class<?> clazz, String path) 读取 jar 内文件为数据流static byte[]resourceToBytes(Class<?> clazz, String path) 读取 jar 内文件为字节数据static FileresourceToDisk(Class<?> clazz, String srcPath, String distPath) 复制 jar 内的文件到磁盘(字节流)static InputStreamresourceToInputStream(Class<?> clazz, String path) 读取 jar 内文件为数据流static StringresourceToString(Class<?> clazz, String path) 读取 jar 内文件为字符串内容(UTF-8)static StringsimpleFileName(File file) 获取简易的文件名,没有格式static byte[]读取文件所有字节static byte[]toBytes(InputStream is) 读取输入流所有字节static void写入字节到文件static voidtoFile(File file, InputStream is) 写入数据流到文件static booleantoFile(File file, InputStream is, IO.OnWriteCallback onWrote) 写入数据流到文件static void序列化对象到文件static void写入字符串到文件static void写入字符串到文件static void写入字符串到文件static FiletoFile(String absFile, InputStream is) 写入数据流到文件static ByteArrayInputStream字节输出流转输入流static InputStreamtoInputStream(RandomAccessFile raf, long from, long size) 读取文件随机访问对象指定区块为输入流,此操作会读取数据至内存,最大 2GB,不会关闭流static <T> T文件反序列化对象static booleantoOutputStream(InputStream is, OutputStream os) 读取输入流并推输出流static voidtoOutputStream(InputStream is, OutputStream os, long start, long end) static booleantoOutputStream(InputStream is, OutputStream os, IO.OnWriteCallback onWrote) 读取输入流并推输出流static booleantoOutputStream(InputStream is, OutputStream os, IO.OnWriteCallback onWrote, com.imyeyu.java.bean.Callback beforeClose) 读取输入流并推输出流static voidtoOutputStream(OutputStream os, byte[] bytes) 写入字节到输出流static booleantoOutputStream(OutputStream os, File file) 读取文件并推到输出流static voidtoOutputStream(OutputStream os, RandomAccessFile raf, long from, long size) 读取文件随机访问对象指定区块到输出流,不会关闭流static voidtoOutputStream(OutputStream os, Object object) 序列化对象到输出流static voidtoOutputStream(OutputStream os, String data) 写入字符串到输出流static voidtoRandomStream(RandomAccessFile raf, byte[] bytes) 写入字节到文件随机访问对象,此操作不会关闭流static voidtoRandomStream(RandomAccessFile raf, InputStream is) 写入数据流到文件随机访问对象,此操作不会关闭流static String读取文件为字符串(UTF-8)static String读取文件为字符串(UTF-8)static String读取文件为字符串static String读取文件为字符串static StringtoString(InputStream is) 读取数据流为字符串(UTF-8)static StringtoString(InputStream is, com.imyeyu.java.bean.CallbackArg<String> onRead) 读取数据流为字符串(UTF-8)static StringtoString(InputStream is, Charset charset, com.imyeyu.java.bean.CallbackArg<String> onRead) 读取数据流为字符串static String[]toStringLines(File file) static voidtoStringLines(File file, com.imyeyu.java.bean.CallbackArg<String> lineCallback) static TreeFile
-
Constructor Details
-
IO
public IO()
-
-
Method Details
-
fitPath
适应路径,在不确定路径结尾是否存在分隔符时总是补充分隔符- Parameters:
path- 路径- Returns:
- 补充分隔符的路径
-
simpleFileName
获取简易的文件名,没有格式- Parameters:
file- 文件- Returns:
- 文件名
-
fileExtension
获取文件扩展名- Parameters:
file- 文件- Returns:
- 扩展名
-
getJarAbsolutePath
运行程序(.jar)所在磁盘的绝对路径,在 debug 环境中为类路径- Parameters:
clazz- 调用方类路径- Returns:
- 绝对路径
-
dir
使用该路径创建文件夹- Parameters:
path- 路径- Returns:
- 文件夹对象
- Throws:
NoPermissionException- 无权限时异常
-
file
获取文件对象,如果不存在则创建该文件- Parameters:
parent- 所属文件夹name- 文件名- Returns:
- 文件
- Throws:
NoPermissionException- 权限异常IOException- 数据流异常
-
file
获取文件对象,如果不存在则创建该文件- Parameters:
path- 文件路径- Returns:
- 文件
- Throws:
NoPermissionException- 权限异常IOException- 数据流异常
-
file
public static File file(String path, com.imyeyu.java.bean.CallbackArg<File> onCreatedFile) throws NoPermissionException, IOException 获取文件对象,如果不存在则创建该文件- Parameters:
path- 文件路径onCreatedFile- 创建回调,不创建时不触发- Returns:
- 文件
- Throws:
NoPermissionException- 权限异常IOException- 数据流异常
-
rename
重命名文件- Parameters:
file- 文件name- 文件名- Returns:
- 重命名后的文件对象
-
toFile
写入字节到文件- Parameters:
file- 文件bytes- 字节- Throws:
IOException- IO 异常NoPermissionException- 权限异常
-
toFile
写入字符串到文件- Parameters:
file- 文件data- 字符串内容- Throws:
IOException- IO 异常NoPermissionException- 权限异常
-
toFile
public static void toFile(File file, String data, String charset) throws IOException, NoPermissionException 写入字符串到文件- Parameters:
file- 文件data- 字符串内容charset- 字符编码- Throws:
IOException- IO 异常NoPermissionException- 权限异常
-
toFile
public static void toFile(File file, String data, Charset charset) throws IOException, NoPermissionException 写入字符串到文件- Parameters:
file- 文件data- 字符串内容charset- 字符编码- Throws:
IOException- IO 异常NoPermissionException- 权限异常
-
toFile
写入数据流到文件- Parameters:
absFile- 文件绝对路径is- 输入流- Returns:
- 文件对象
- Throws:
IOException- IO 异常NoPermissionException- 权限异常
-
toFile
写入数据流到文件- Parameters:
file- 文件is- 输入流- Throws:
IOException- IO 异常NoPermissionException- 权限异常
-
toFile
public static boolean toFile(File file, InputStream is, IO.OnWriteCallback onWrote) throws IOException, NoPermissionException 写入数据流到文件- Parameters:
file- 文件is- 输入流onWrote- 写入文件回调,参数为已写入数据量,返回 true 时继续传输,否则中断流传输- Returns:
- true 为正常传输完成,否则发生中断传输
- Throws:
IOException- IO 异常NoPermissionException- 权限异常
-
toFile
序列化对象到文件- Parameters:
file- 文件object- 可序列化对象- Throws:
IOException- IO 异常NoPermissionException- 权限异常
-
toObject
文件反序列化对象- Type Parameters:
T- 类型- Parameters:
file- 文件clazz- 对象类型- Returns:
- 对象
- Throws:
IOException- IO 异常ClassNotFoundException- 强制类型转换异常
-
toStringLines
- Throws:
IOException
-
toStringLines
public static void toStringLines(File file, com.imyeyu.java.bean.CallbackArg<String> lineCallback) throws IOException - Throws:
IOException
-
toString
读取文件为字符串(UTF-8)- Parameters:
file- 文件- Returns:
- 文件内容
- Throws:
IOException- IO 异常
-
toString
public static String toString(File file, com.imyeyu.java.bean.CallbackArg<String> onRead) throws IOException 读取文件为字符串(UTF-8)- Parameters:
file- 文件onRead- 读取回调- Returns:
- 文件内容
- Throws:
IOException- IO 异常
-
toString
读取文件为字符串- Parameters:
file- 文件charset- 编码类型- Returns:
- 文件内容
- Throws:
IOException- IO 异常
-
toString
public static String toString(File file, Charset charset, com.imyeyu.java.bean.CallbackArg<String> onWrote) throws IOException 读取文件为字符串- Parameters:
file- 文件charset- 编码类型onWrote- 读取回调- Returns:
- 文件内容
- Throws:
IOException- IO 异常
-
toString
读取数据流为字符串(UTF-8)- Parameters:
is- 输入流- Returns:
- 字符串
- Throws:
IOException- IO 异常
-
toString
public static String toString(InputStream is, com.imyeyu.java.bean.CallbackArg<String> onRead) throws IOException 读取数据流为字符串(UTF-8)- Parameters:
is- 输入流onRead- 读取回调- Returns:
- 字符串
- Throws:
IOException- IO 异常
-
toString
public static String toString(InputStream is, Charset charset, com.imyeyu.java.bean.CallbackArg<String> onRead) throws IOException 读取数据流为字符串- Parameters:
is- 输入流charset- 编码格式onRead- 读取回调- Returns:
- 字符串
- Throws:
IOException- IO 异常
-
getInputStream
获取文件数据流- Parameters:
path- 文件路径- Returns:
- 数据流
- Throws:
FileNotFoundException- 找不到文件
-
getInputStream
获取文件数据流- Parameters:
file- 文件- Returns:
- 数据流
- Throws:
FileNotFoundException- 找不到文件
-
getRandomAccessFile
获取文件随机访问对象- Parameters:
path- 文件路径- Returns:
- 随机范文对象
- Throws:
FileNotFoundException- 找不到文件
-
getRandomAccessFile
获取文件随机访问对象- Parameters:
file- 文件- Returns:
- 随机访问对象
- Throws:
FileNotFoundException- 找不到文件
-
getOutputStream
获取文件的输出流(如果不存在将会创建文件)- Parameters:
path- 文件路径- Returns:
- 输出流
- Throws:
IOException- IO 异常NoPermissionException- 权限异常
-
getOutputStream
获取文件的输出流(如果不存在将会创建文件)- Parameters:
file- 文件- Returns:
- 输出流
- Throws:
IOException- IO 异常NoPermissionException- 权限异常
-
toOutputStream
写入字符串到输出流- Parameters:
os- 输出流data- 字符串- Throws:
IOException- IO 异常
-
toOutputStream
写入字节到输出流- Parameters:
os- 输出流bytes- 字节- Throws:
IOException- IO 异常
-
toOutputStream
读取文件并推到输出流- Parameters:
os- 输出流file- 文件- Returns:
- true 为正常传输完成,否则发生中断传输
- Throws:
IOException- IO 异常
-
toOutputStream
读取输入流并推输出流- Parameters:
is- 输入流os- 输出流- Returns:
- true 为正常传输完成,否则发生中断传输
- Throws:
IOException- IO 异常
-
toOutputStream
public static boolean toOutputStream(InputStream is, OutputStream os, IO.OnWriteCallback onWrote) throws IOException 读取输入流并推输出流- Parameters:
is- 输入流os- 输出流onWrote- 写入回调,参数为已写入数据量,返回 true 时继续传输,否则中断流传输- Returns:
- true 为正常传输完成,否则发生中断传输
- Throws:
IOException- IO 异常
-
toOutputStream
public static boolean toOutputStream(InputStream is, OutputStream os, IO.OnWriteCallback onWrote, com.imyeyu.java.bean.Callback beforeClose) throws IOException 读取输入流并推输出流- Parameters:
is- 输入流os- 输出流onWrote- 写入回调,参数为已写入数据量,返回 true 时继续传输,否则中断流传输beforeClose- 流关闭前事件- Returns:
- true 为正常传输完成,否则发生中断传输
- Throws:
IOException- IO 异常
-
toOutputStream
public static void toOutputStream(InputStream is, OutputStream os, long start, long end) throws IOException - Throws:
IOException
-
toRandomStream
写入字节到文件随机访问对象,此操作不会关闭流- Parameters:
raf- 文件随机访问的对象bytes- 字节- Throws:
IOException- IO 异常
-
toRandomStream
写入数据流到文件随机访问对象,此操作不会关闭流- Parameters:
raf- 文件随机访问对象is- 数据流- Throws:
IOException- IO 异常
-
toInputStream
字节输出流转输入流- Parameters:
baos- 输出流- Returns:
- 输入流
-
toInputStream
public static InputStream toInputStream(RandomAccessFile raf, long from, long size) throws IOException 读取文件随机访问对象指定区块为输入流,此操作会读取数据至内存,最大 2GB,不会关闭流- Parameters:
raf- 文件随机访问对象from- 开始字节size- 读取大小,-1 时将读到结尾- Returns:
- 读取数据流
- Throws:
IOException- IO 异常
-
toOutputStream
public static void toOutputStream(OutputStream os, RandomAccessFile raf, long from, long size) throws IOException 读取文件随机访问对象指定区块到输出流,不会关闭流- Parameters:
os- 输出流raf- 文件随机访问对象from- 开始字节size- 读取大小,-1 时将读到结尾- Throws:
IOException- IO 异常
-
toOutputStream
序列化对象到输出流- Parameters:
os- 输出流object- 可序列化对象- Throws:
IOException- IO 异常
-
treeFile
-
listFile
深度遍历文件夹获取所有文件- Parameters:
file- 文件夹- Returns:
- 所有文件
-
copy
- Throws:
NoPermissionExceptionIOException
-
copy
public static void copy(File from, String toPath, IO.OnWriteCallback callback) throws NoPermissionException, IOException 复制文件- Parameters:
from- 源文件toPath- 目标文件夹路径- Throws:
NoPermissionException- 无权限IOException- IO 异常
-
move
移动文件- Parameters:
from- 源文件toPath- 目标路径- Throws:
NoPermissionException- 无权限IOException- IO 异常
-
destroy
删除文件或文件夹,删除失败不响应- Parameters:
file- 文件
-
resourceToInputStream
读取 jar 内文件为数据流- Parameters:
path- jar 内文件路径,不需要 / 开始,如 config/TimiJava.ini- Returns:
- 数据流
-
resourceExist
读取 jar 内文件为数据流- Parameters:
path- jar 内文件路径,不需要 / 开始,如 config/TimiJava.ini- Returns:
- 数据流
- Throws:
IOException
-
resourceToBytes
读取 jar 内文件为字节数据- Parameters:
path- jar 内文件路径,不需要 / 开始,如 config/TimiJava.ini- Returns:
- 字节数据
- Throws:
IOException
-
resourceToString
读取 jar 内文件为字符串内容(UTF-8)- Parameters:
path- jar 内文件路径,不需要 / 开始,如 config/TimiJava.ini- Returns:
- 字符串内容
-
resourceToDisk
public static File resourceToDisk(Class<?> clazz, String srcPath, String distPath) throws NoPermissionException 复制 jar 内的文件到磁盘(字节流)- Parameters:
srcPath- jar 内文件路径,不需要 / 开始,如 config/TimiJava.inidistPath- 磁盘路径- Returns:
- 磁盘文件
- Throws:
NoPermissionException- 权限异常
-
toBytes
读取文件所有字节- Parameters:
file- 输入流- Returns:
- 字节数据
- Throws:
IOException- IO 异常
-
toBytes
读取输入流所有字节- Parameters:
is- 输入流- Returns:
- 字节数据
- Throws:
IOException
-
bytesClone
public static byte[] bytesClone(byte[] bytes) 字节数组克隆- Parameters:
bytes- 输入流- Returns:
- 字节数据
-
hideFile
隐藏文件- Parameters:
files- 文件列表- Throws:
IOException- IO 异常
-
calcSize
计算文件或文件夹大小- Parameters:
file- 文件或文件夹- Returns:
- 总大小(字节)
-
md5
计算文件 MD5- Parameters:
file- 文件- Returns:
- MD5
- Throws:
FileNotFoundException
-
md5
计算输入流 MD5- Parameters:
stream- 输入流- Returns:
- MD5
-