Package com.imyeyu.io

Class IO

java.lang.Object
com.imyeyu.io.IO
All Implemented Interfaces:
com.imyeyu.utils.OS.FileSystem

public class IO extends Object implements com.imyeyu.utils.OS.FileSystem
文件和数据流操作
Version:
2021-02-13 10:15
Author:
夜雨
  • Constructor Details

    • IO

      public IO()
  • Method Details

    • fitPath

      public static String fitPath(String path)
      适应路径,在不确定路径结尾是否存在分隔符时总是补充分隔符
      Parameters:
      path - 路径
      Returns:
      补充分隔符的路径
    • simpleFileName

      public static String simpleFileName(File file)
      获取简易的文件名,没有格式
      Parameters:
      file - 文件
      Returns:
      文件名
    • fileExtension

      public static String fileExtension(File file)
      获取文件扩展名
      Parameters:
      file - 文件
      Returns:
      扩展名
    • getJarAbsolutePath

      public static String getJarAbsolutePath(Class<?> clazz)
      运行程序(.jar)所在磁盘的绝对路径,在 debug 环境中为类路径
      Parameters:
      clazz - 调用方类路径
      Returns:
      绝对路径
    • dir

      public static File dir(String path) throws NoPermissionException
      使用该路径创建文件夹
      Parameters:
      path - 路径
      Returns:
      文件夹对象
      Throws:
      NoPermissionException - 无权限时异常
    • file

      public static File file(File parent, String name) throws NoPermissionException, IOException
      获取文件对象,如果不存在则创建该文件
      Parameters:
      parent - 所属文件夹
      name - 文件名
      Returns:
      文件
      Throws:
      NoPermissionException - 权限异常
      IOException - 数据流异常
    • file

      public static File file(String path) throws NoPermissionException, IOException
      获取文件对象,如果不存在则创建该文件
      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

      public static File rename(File file, String name)
      重命名文件
      Parameters:
      file - 文件
      name - 文件名
      Returns:
      重命名后的文件对象
    • toFile

      public static void toFile(File file, byte[] bytes) throws IOException, NoPermissionException
      写入字节到文件
      Parameters:
      file - 文件
      bytes - 字节
      Throws:
      IOException - IO 异常
      NoPermissionException - 权限异常
    • toFile

      public static void toFile(File file, String data) throws IOException, NoPermissionException
      写入字符串到文件
      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

      public static File toFile(String absFile, InputStream is) throws IOException, NoPermissionException
      写入数据流到文件
      Parameters:
      absFile - 文件绝对路径
      is - 输入流
      Returns:
      文件对象
      Throws:
      IOException - IO 异常
      NoPermissionException - 权限异常
    • toFile

      public static void toFile(File file, InputStream is) throws IOException, NoPermissionException
      写入数据流到文件
      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

      public static void toFile(File file, Object object) throws IOException, NoPermissionException
      序列化对象到文件
      Parameters:
      file - 文件
      object - 可序列化对象
      Throws:
      IOException - IO 异常
      NoPermissionException - 权限异常
    • toObject

      public static <T> T toObject(File file, Class<T> clazz) throws IOException, ClassNotFoundException
      文件反序列化对象
      Type Parameters:
      T - 类型
      Parameters:
      file - 文件
      clazz - 对象类型
      Returns:
      对象
      Throws:
      IOException - IO 异常
      ClassNotFoundException - 强制类型转换异常
    • toStringLines

      public static String[] toStringLines(File file) throws IOException
      Throws:
      IOException
    • toStringLines

      public static void toStringLines(File file, com.imyeyu.java.bean.CallbackArg<String> lineCallback) throws IOException
      Throws:
      IOException
    • toString

      public static String toString(File file) throws IOException
      读取文件为字符串(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

      public static String toString(File file, Charset charset) throws IOException
      读取文件为字符串
      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

      public static String toString(InputStream is) throws IOException
      读取数据流为字符串(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

      public static InputStream getInputStream(String path) throws FileNotFoundException
      获取文件数据流
      Parameters:
      path - 文件路径
      Returns:
      数据流
      Throws:
      FileNotFoundException - 找不到文件
    • getInputStream

      public static InputStream getInputStream(File file) throws FileNotFoundException
      获取文件数据流
      Parameters:
      file - 文件
      Returns:
      数据流
      Throws:
      FileNotFoundException - 找不到文件
    • getRandomAccessFile

      public static RandomAccessFile getRandomAccessFile(String path) throws FileNotFoundException
      获取文件随机访问对象
      Parameters:
      path - 文件路径
      Returns:
      随机范文对象
      Throws:
      FileNotFoundException - 找不到文件
    • getRandomAccessFile

      public static RandomAccessFile getRandomAccessFile(File file) throws FileNotFoundException
      获取文件随机访问对象
      Parameters:
      file - 文件
      Returns:
      随机访问对象
      Throws:
      FileNotFoundException - 找不到文件
    • getOutputStream

      public static OutputStream getOutputStream(String path) throws IOException, NoPermissionException
      获取文件的输出流(如果不存在将会创建文件)
      Parameters:
      path - 文件路径
      Returns:
      输出流
      Throws:
      IOException - IO 异常
      NoPermissionException - 权限异常
    • getOutputStream

      public static OutputStream getOutputStream(File file) throws IOException, NoPermissionException
      获取文件的输出流(如果不存在将会创建文件)
      Parameters:
      file - 文件
      Returns:
      输出流
      Throws:
      IOException - IO 异常
      NoPermissionException - 权限异常
    • toOutputStream

      public static void toOutputStream(OutputStream os, String data) throws IOException
      写入字符串到输出流
      Parameters:
      os - 输出流
      data - 字符串
      Throws:
      IOException - IO 异常
    • toOutputStream

      public static void toOutputStream(OutputStream os, byte[] bytes) throws IOException
      写入字节到输出流
      Parameters:
      os - 输出流
      bytes - 字节
      Throws:
      IOException - IO 异常
    • toOutputStream

      public static boolean toOutputStream(OutputStream os, File file) throws IOException
      读取文件并推到输出流
      Parameters:
      os - 输出流
      file - 文件
      Returns:
      true 为正常传输完成,否则发生中断传输
      Throws:
      IOException - IO 异常
    • toOutputStream

      public static boolean toOutputStream(InputStream is, OutputStream os) throws IOException
      读取输入流并推输出流
      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

      public static void toRandomStream(RandomAccessFile raf, byte[] bytes) throws IOException
      写入字节到文件随机访问对象,此操作不会关闭流
      Parameters:
      raf - 文件随机访问的对象
      bytes - 字节
      Throws:
      IOException - IO 异常
    • toRandomStream

      public static void toRandomStream(RandomAccessFile raf, InputStream is) throws IOException
      写入数据流到文件随机访问对象,此操作不会关闭流
      Parameters:
      raf - 文件随机访问对象
      is - 数据流
      Throws:
      IOException - IO 异常
    • toInputStream

      public static ByteArrayInputStream toInputStream(ByteArrayOutputStream baos)
      字节输出流转输入流
      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

      public static void toOutputStream(OutputStream os, Object object) throws IOException
      序列化对象到输出流
      Parameters:
      os - 输出流
      object - 可序列化对象
      Throws:
      IOException - IO 异常
    • treeFile

      public static TreeFile treeFile(String path)
    • listFile

      public static List<File> listFile(File file)
      深度遍历文件夹获取所有文件
      Parameters:
      file - 文件夹
      Returns:
      所有文件
    • copy

      public static void copy(File from, String toPath) throws NoPermissionException, IOException
      Throws:
      NoPermissionException
      IOException
    • copy

      public static void copy(File from, String toPath, IO.OnWriteCallback callback) throws NoPermissionException, IOException
      复制文件
      Parameters:
      from - 源文件
      toPath - 目标文件夹路径
      Throws:
      NoPermissionException - 无权限
      IOException - IO 异常
    • move

      public static void move(File from, String toPath) throws NoPermissionException, IOException
      移动文件
      Parameters:
      from - 源文件
      toPath - 目标路径
      Throws:
      NoPermissionException - 无权限
      IOException - IO 异常
    • destroy

      public static void destroy(File file)
      删除文件或文件夹,删除失败不响应
      Parameters:
      file - 文件
    • resourceToInputStream

      public static InputStream resourceToInputStream(Class<?> clazz, String path)
      读取 jar 内文件为数据流
      Parameters:
      path - jar 内文件路径,不需要 / 开始,如 config/TimiJava.ini
      Returns:
      数据流
    • resourceExist

      public static boolean resourceExist(Class<?> clazz, String path) throws IOException
      读取 jar 内文件为数据流
      Parameters:
      path - jar 内文件路径,不需要 / 开始,如 config/TimiJava.ini
      Returns:
      数据流
      Throws:
      IOException
    • resourceToBytes

      public static byte[] resourceToBytes(Class<?> clazz, String path) throws IOException
      读取 jar 内文件为字节数据
      Parameters:
      path - jar 内文件路径,不需要 / 开始,如 config/TimiJava.ini
      Returns:
      字节数据
      Throws:
      IOException
    • resourceToString

      public static String resourceToString(Class<?> clazz, String path)
      读取 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.ini
      distPath - 磁盘路径
      Returns:
      磁盘文件
      Throws:
      NoPermissionException - 权限异常
    • toBytes

      public static byte[] toBytes(File file) throws IOException
      读取文件所有字节
      Parameters:
      file - 输入流
      Returns:
      字节数据
      Throws:
      IOException - IO 异常
    • toBytes

      public static byte[] toBytes(InputStream is) throws IOException
      读取输入流所有字节
      Parameters:
      is - 输入流
      Returns:
      字节数据
      Throws:
      IOException
    • bytesClone

      public static byte[] bytesClone(byte[] bytes)
      字节数组克隆
      Parameters:
      bytes - 输入流
      Returns:
      字节数据
    • hideFile

      public static void hideFile(File... files) throws IOException
      隐藏文件
      Parameters:
      files - 文件列表
      Throws:
      IOException - IO 异常
    • calcSize

      public static long calcSize(File file)
      计算文件或文件夹大小
      Parameters:
      file - 文件或文件夹
      Returns:
      总大小(字节)
    • md5

      public static String md5(File file) throws FileNotFoundException
      计算文件 MD5
      Parameters:
      file - 文件
      Returns:
      MD5
      Throws:
      FileNotFoundException
    • md5

      public static String md5(InputStream stream)
      计算输入流 MD5
      Parameters:
      stream - 输入流
      Returns:
      MD5