Class FileTool

java.lang.Object
com.xxl.tool.io.FileTool

public class FileTool extends Object
File Tool
Author:
xuxueli 2024-10-27
  • Constructor Details

    • FileTool

      public FileTool()
  • Method Details

    • file

      public static File file(String path)
      create new file object, not actual file on disk
      Parameters:
      path - file path
      Returns:
      file object
    • file

      public static File file(String parent, String child)
      create new file object, not actual file on disk
      Parameters:
      parent - parent path
      child - child path
      Returns:
      file object
    • createFile

      public static File createFile(String path) throws IOException
      create actual file on disk
      Parameters:
      path - file path
      Returns:
      file
      Throws:
      IOException
    • createFile

      public static File createFile(String parent, String child) throws IOException
      create actual file on disk
      Parameters:
      parent - parent path
      child - child path
      Returns:
      file
      Throws:
      IOException
    • createFile

      public static File createFile(File file) throws IOException
      create actual file on disk
      Parameters:
      file - file object
      Returns:
      file
      Throws:
      IOException
    • createParentDirectories

      public static boolean createParentDirectories(File file) throws IOException
      create parent directories of file 1、will create all necessary parent directories
      Parameters:
      file - file object
      Throws:
      IOException
    • createDirectories

      public static boolean createDirectories(File dir) throws IOException
      create directories 1、will create all necessary parent directories
      Parameters:
      dir - dir
      Returns:
      true if and only if the directory was created, along with all necessary parent directories; false otherwise
      Throws:
      IOException
    • isDirectory

      public static boolean isDirectory(String path)
      is directory
      Parameters:
      path - path of file
      Returns:
      true if is directory
    • isDirectory

      public static boolean isDirectory(File file)
      is directory
      Parameters:
      file - file
      Returns:
      true if is directory
    • isFile

      public static boolean isFile(String path)
      is file
      Parameters:
      path - path of file
      Returns:
      true if is file
    • isFile

      public static boolean isFile(File file)
      is file
      Parameters:
      file - file object
      Returns:
      true if is file
    • isEmpty

      public static boolean isEmpty(File file)
      is empty file or directory
      Parameters:
      file - file object
      Returns:
      true if empty
    • isNotEmpty

      public static boolean isNotEmpty(File file)
      is not empty, file or directory
      Parameters:
      file - file object
      Returns:
      true if is not empty
    • isSameFile

      public static boolean isSameFile(File file1, File file2) throws IOException
      is same file
      Parameters:
      file1 - file1 object
      file2 - file2 object
      Returns:
      true if is same file
      Throws:
      IOException
    • isSub

      public static boolean isSub(Path parent, Path sub)
      is sub file of parent
      Parameters:
      parent - parent file
      sub - sub file
      Returns:
      true if is sub file of parent
    • exists

      public static boolean exists(String path)
      is file exist
      Parameters:
      path - path of file
      Returns:
      true if file exist
    • exists

      public static boolean exists(File file)
      whether a file exists
      Parameters:
      file - file object
      Returns:
      true if file exist
    • toAbsoluteNormal

      public static Path toAbsoluteNormal(Path path)
      to absolute normal path
      Parameters:
      path - file path
      Returns:
      absolute normal path
    • toAbsolutePath

      public static Path toAbsolutePath(Path path)
      to absolute path
      Parameters:
      path - file path
      Returns:
      absolute path
    • size

      public static long size(String path)
      get file size, not include directory size
      Parameters:
      path - file path
      Returns:
      size in bytes
    • size

      public static long size(File file)
      get file size, not include directory size
      Parameters:
      file - file object
      Returns:
      size in bytes
    • size

      public static long size(File file, boolean includeDirSize)
      get file size
      Parameters:
      file - file object
      includeDirSize - include directory size
      Returns:
      size in bytes
    • totalLines

      public static int totalLines(String path)
      get total lines
      Parameters:
      path - path of file
      Returns:
      total line number
    • totalLines

      public static int totalLines(File file)
      get total lines
      Parameters:
      file - file object
      Returns:
      total line number
    • totalLines

      public static int totalLines(File file, int bufferSize, boolean lastLineSeparatorAsLine)
      get total lines
      Parameters:
      file - file
      bufferSize - buffer size
      lastLineSeparatorAsLine - last line separator as line
      Returns:
      total line number
    • delete

      public static boolean delete(String path)
      delete file or directory
      Parameters:
      path - path of file
      Returns:
      true if success
    • delete

      public static boolean delete(File file)
      delete file or directory
      Parameters:
      file - file object
      Returns:
      true if success
    • clean

      public static boolean clean(String dirPath)
      clean directory
      Parameters:
      dirPath - directory path
      Returns:
      true if success
    • clean

      public static boolean clean(File directory)
      clean directory
      Parameters:
      directory - directory file object
      Returns:
      true if success
    • clean

      public static boolean clean(File directory, boolean skipDeleteFailFile)
      clean directory
      Parameters:
      directory - directory file object
      skipDeleteFailFile - true, skip delete fail file
      Returns:
    • copy

      public static File copy(String src, String dest, boolean isOverride) throws IOException
      copy file
      Parameters:
      src - source file path
      dest - destination file path or directory path
      isOverride - true will override, false and dest-file exists will throw FileAlreadyExistsException
      Returns:
      destination file
      Throws:
      IOException
    • copy

      public static File copy(File src, File dest, boolean isOverride) throws IOException
      copy file
      Parameters:
      src - source file
      dest - destination file or directory
      isOverride - true will override, false and dest-file exists will throw FileAlreadyExistsException
      Returns:
      destination file
      Throws:
      IOException
    • copy

      public static File copy(Path src, Path dest, boolean isOverride) throws IOException
      copy file
      Parameters:
      src - source file
      dest - destination file or directory
      isOverride - true will override, false and dest-file exists will throw FileAlreadyExistsException
      Returns:
      destination file
      Throws:
      IOException
    • copy

      public static File copy(Path src, Path dest, StandardCopyOption... options) throws IOException
      copy file 1、source must exists 2、source only support file (todo: support directory) 3、destination file can not be same as source file 4、destination can not be sub of source
      Parameters:
      src - source file path
      dest - destination file path or directory path
      options - copy options, such as StandardCopyOption.REPLACE_EXISTING、StandardCopyOption.COPY_ATTRIBUTES
      Returns:
      destination file
      Throws:
      IOException
    • move

      public static File move(String src, String dest, boolean isOverride) throws IOException
      move file
      Parameters:
      src - source file path
      dest - destination file path or directory path
      isOverride - true will override, false and dest-file exists will throw FileAlreadyExistsException
      Returns:
      destination file
      Throws:
      IOException
    • move

      public static File move(File src, File dest, boolean isOverride) throws IOException
      move file or directory
      Parameters:
      src - source file
      dest - destination file or directory
      isOverride - true will override, false and dest-file exists will throw FileAlreadyExistsException
      Returns:
      destination file
      Throws:
      IOException
    • move

      public static File move(Path src, Path dest, boolean isOverride) throws IOException
      move file or directory
      Parameters:
      src - source file
      dest - destination file or directory
      isOverride - true will override, false and dest-file exists will throw FileAlreadyExistsException
      Returns:
      destination file
      Throws:
      IOException
    • move

      public static File move(Path src, Path dest, StandardCopyOption... options) throws IOException
      move file or directory 1、source must exists 2、destination file can not be same as source file 3、destination can not be sub of source
      Parameters:
      src - source file path
      dest - destination file path or directory path
      options - copy options, such as StandardCopyOption.REPLACE_EXISTING、StandardCopyOption.COPY_ATTRIBUTES
      Returns:
      destination file
      Throws:
      IOException
    • rename

      public static File rename(File file, String newFileName, boolean isOverride) throws IOException
      rename file
      Parameters:
      file - origin file
      newFileName - new file name
      isOverride - true if override, false will skip
      Returns:
      new file
      Throws:
      IOException
    • writeString

      public static void writeString(String path, String content) throws IOException
      write string
      Parameters:
      path - file path
      content - file content
      Throws:
      IOException
    • writeString

      public static void writeString(String path, String content, boolean append) throws IOException
      write string
      Parameters:
      path - file path
      content - file content
      append - true if append, false will override
      Throws:
      IOException
    • writeString

      public static void writeString(String path, String content, boolean append, Charset charset) throws IOException
      write string
      Parameters:
      path - file path
      content - file content
      append - true if append, false will override
      charset - charset encoding
      Throws:
      IOException
    • writeLines

      public static void writeLines(String path, Iterable<?> lines) throws IOException
      write lines 1、default append
      Parameters:
      path - file path
      lines - lines content
      Throws:
      IOException - exception
    • writeLines

      public static void writeLines(String path, Iterable<?> lines, boolean append) throws IOException
      write lines
      Parameters:
      path - file path
      lines - lines content
      append - true if append, false will override
      Throws:
      IOException - exception
    • writeLines

      public static void writeLines(String path, Iterable<?> lines, String lineSeparator, boolean append, Charset charset) throws IOException
      write lines
      Parameters:
      path - file path
      lines - lines content
      lineSeparator - line separator
      append - true if append, false will override
      charset - charset encoding
      Throws:
      IOException - exception
    • writeLines

      public static void writeLines(String path, Supplier<?> lineSupplier) throws IOException
      write lines 1、default append
      Parameters:
      path - file path
      lineSupplier - line supplier
      Throws:
      IOException - exception
    • writeLines

      public static void writeLines(String path, Supplier<?> lineSupplier, boolean append) throws IOException
      write lines
      Parameters:
      path - file path
      lineSupplier - line supplier
      append - true if append, false will override
      Throws:
      IOException - exception
    • writeLines

      public static void writeLines(String path, Supplier<?> lineSupplier, String lineSeparator, boolean append, Charset charset) throws IOException
      write lines
      Parameters:
      path - file path
      lineSupplier - line supplier
      lineSeparator - line separator
      append - true if append, false will override
      charset - charset encoding
      Throws:
      IOException - exception
    • readString

      public static String readString(String path) throws IOException
      read string
      Parameters:
      path - file path
      Returns:
      file data string
      Throws:
      IOException
    • readString

      public static String readString(String path, Charset charset) throws IOException
      read string
      Parameters:
      path - file path
      charset - charset encoding
      Returns:
      file data string
      Throws:
      IOException
    • readLines

      public static List<String> readLines(String path) throws IOException
      read lines
      Parameters:
      path - file path
      Returns:
      file data lines
      Throws:
      IOException - exception
    • readLines

      public static List<String> readLines(String path, Charset charset) throws IOException
      read lines
      Parameters:
      path - file path
      charset - charset encoding
      Returns:
      file data lines
      Throws:
      IOException - exception
    • readLines

      public static void readLines(String path, Consumer<String> lineConsumer) throws IOException
      read lines with consumer
      Parameters:
      path - file path
      lineConsumer - line consumer
      Throws:
      IOException - exception
    • readLines

      public static void readLines(String path, Charset charset, Consumer<String> lineConsumer) throws IOException
      read lines with consumer
      Parameters:
      path - file path
      charset - charset encoding
      lineConsumer - line consumer
      Throws:
      IOException - exception