Class FileUtil

java.lang.Object
com.acanx.utils.FileUtil

public class FileUtil extends Object
ACANX-Util / com.acanx.utils / FileUtil 文件由 ACANX 创建于 2019/1/5 . 15:47 FileUtil: 补充说明: 2019/1/5 15:47
Since:
0.0.1
Author:
ACANX
  • Method Details

    • getSysTempDir

      public static String getSysTempDir()
      获取系统临时目录
      Returns:
      系统临时目录路径字符串
    • fileExists

      @Alpha public static boolean fileExists(File file)
      文件是否存在
      Parameters:
      file - 文件
      Returns:
      判断结果
    • moveFile

      @Alpha public static void moveFile(File src, File dest) throws IOException
      移动文件
      Parameters:
      src - 源文件
      dest - 目标文件
      Throws:
      IOException - 如果移动文件时发生错误
    • deleteFile

      @Alpha public static boolean deleteFile(File file)
      删除目标文件
      Parameters:
      file - 目标文件
      Returns:
      文件删除结果
    • getFileList

      @Alpha public static List<File> getFileList(File dir)
      重载方法,支持传入多个目录
      Parameters:
      dir - 入口文件夹
      Returns:
      文件集合
    • getFilteredFileList

      @Alpha public static List<File> getFilteredFileList(File dir, FileFilter fileFilter)
      获取过滤后的文件列表
      Parameters:
      dir - 入口文件
      fileFilter - 过滤器实现类
      Returns:
      过滤后的文件集合
    • getFileMD5

      @Alpha public static String getFileMD5(File file)
      计算文件的MD5
      Parameters:
      file - 需要计算的文件
      Returns:
      计算的文件MD5值
    • getFileSHA1

      @Alpha public static String getFileSHA1(File file)
      计算文件的SHA1
      Parameters:
      file - 需要计算SHA1的文件
      Returns:
      文件的SHA1值
    • readFileAsBytes

      public static byte[] readFileAsBytes(String filePath) throws IOException
      根据文件路径读取byte[] 数组
      Parameters:
      filePath - 文件路径
      Returns:
      文本内容,byte[]类型
      Throws:
      IOException - IO异常
    • readLines

      @Alpha public static List<String> readLines(File file) throws IOException
      Reads the contents of a file line by line to a List of Strings. The file is always closed. Use Default CharSet UTF-8
      Parameters:
      file - the file to read, must not be null
      Returns:
      the list of Strings representing each line in the file, never null
      Throws:
      NullPointerException - if file is null.
      IOException - if an I/O error occurs, including when the file does not exist, is a directory rather than a regular file, or for some other reason why the file cannot be opened for reading.
      UnsupportedCharsetException - if the named charset is unavailable.
      Since:
      0.0.1.10
    • readLines

      @Alpha public static List<String> readLines(File file, String charsetName) throws IOException
      Reads the contents of a file line by line to a List of Strings. The file is always closed.
      Parameters:
      file - the file to read, must not be null
      charsetName - the name of the requested charset, null means platform default
      Returns:
      the list of Strings representing each line in the file, never null
      Throws:
      NullPointerException - if file is null.
      IOException - if an I/O error occurs, including when the file does not exist, is a directory rather than a regular file, or for some other reason why the file cannot be opened for reading.
      UnsupportedCharsetException - if the named charset is unavailable.
      Since:
      0.0.1.10
    • readLines

      @Alpha public static List<String> readLines(File file, Charset charset) throws IOException
      按行读取文件,将其保存到字符串集合中返回
      Parameters:
      file - 需要读的文件
      charset - 文件字符集
      Returns:
      字符串集合 一行一项
      Throws:
      IOException - IOException
      Since:
      0.0.1.10
    • readFileAsString

      public static String readFileAsString(String filePath) throws IOException
      读取文件内容,作为字符串返回
      Parameters:
      filePath - 文件路径
      Returns:
      文件内容
      Throws:
      IOException - 文件IO异常
      Since:
      0.0.1.10
    • readFileToString

      @Alpha public static String readFileToString(File file) throws IOException
      Throws:
      IOException
    • readFileToString

      @Alpha public static String readFileToString(File file, String charsetName) throws IOException
      Reads the contents of a file into a String. The file is always closed.
      Parameters:
      file - the file to read, must not be null
      charsetName - the name of the requested charset, null means platform default
      Returns:
      the file contents, never null
      Throws:
      NullPointerException - if file is null.
      IOException - if an I/O error occurs, including when the file does not exist, is a directory rather than a regular file, or for some other reason why the file cannot be opened for reading.
      UnsupportedCharsetException - if the named charset is unavailable.
      Since:
      0.0.1.10
    • readFileToString

      @Alpha public static String readFileToString(File file, Charset charset) throws IOException
      Reads the contents of a file into a String. The file is always closed.
      Parameters:
      file - the file to read, must not be null
      charset - the requested charset, null means platform default
      Returns:
      the file contents, never null
      Throws:
      NullPointerException - if file is null.
      IOException - if an I/O error occurs, including when the file does not exist, is a directory rather than a regular file, or for some other reason why the file cannot be opened for reading.
      Since:
      0.0.1.10
    • write

      @Alpha public static void write(File file, CharSequence data) throws IOException
      Writes a CharSequence to a file creating the file if it does not exist.
      Parameters:
      file - the file to write
      data - the content to write to the file
      Throws:
      IOException - in case of an I/O error
      Since:
      0.0.1.11
    • write

      @Alpha public static void write(File file, CharSequence data, Charset charset) throws IOException
      Writes a CharSequence to a file creating the file if it does not exist.
      Parameters:
      file - the file to write
      data - the content to write to the file
      charset - the name of the requested charset, null means platform default
      Throws:
      IOException - in case of an I/O error
      Since:
      0.0.1.10
    • writeLines

      @Alpha public static void writeLines(File file, Collection<?> lines, Charset charset) throws IOException
      按行写入字符串集合
      Parameters:
      file - 文件
      lines - lines
      charset - 字符集
      Throws:
      IOException - IOException
      Since:
      0.0.1.10
    • getSortedFilesByLastModified

      public static File[] getSortedFilesByLastModified(String directory)
      获取按最后修改时间排序的文件列表
      Parameters:
      directory - 文件夹路径
      Returns:
      按最后修改时间排序的文件数组 最早修改的在前,最近修改/写入的排在最后
    • getFileListSortedByLastModified

      public static List<File> getFileListSortedByLastModified(String directory)
      获取按最后修改时间排序的文件列表
      Parameters:
      directory - 文件夹路径
      Returns:
      按最后修改时间(正序)排序的文件列表