Class FileUtil

java.lang.Object
org.rapidcontext.util.FileUtil

public final class FileUtil extends Object
A set of utility methods for handling files.
Version:
1.0
  • Method Details

    • canonical

      public static File canonical(File file)
      Attempts to resolve a file to a canonical file.
      Parameters:
      file - the file to resolve
      Returns:
      the canonical file, or the original file if the resolution failed
    • resolve

      public static File resolve(File dir, String child)
      Finds a child file in a directory by case-insensitive search. If no file is found, a new file object is created.
      Parameters:
      dir - the parent directory
      child - the child file name
      Returns:
      the corresponding file object
    • readText

      public static String readText(File file) throws IOException
      Reads a file containing UTF-8 text content.
      Parameters:
      file - the input file to read
      Returns:
      the text content of the file
      Throws:
      IOException - if the file couldn't be read
    • readText

      public static String readText(InputStream is) throws IOException
      Reads an input stream containing UTF-8 text content.
      Parameters:
      is - the input stream to read
      Returns:
      the text content of the input stream
      Throws:
      IOException - if the input stream couldn't be read
    • copy

      public static void copy(File src, File dst) throws IOException
      Copies a file or a directory. Directories are copied recursively and file modification dates are kept. If the executable bit is set on the source file, that bit will also be set on the destination file.
      Parameters:
      src - the source file or directory
      dst - the destination file or directory
      Throws:
      IOException - if some file couldn't be copied
    • copy

      public static void copy(InputStream is, File dst) throws IOException
      Copies the data from an input stream to a destination file. If the file exists, it will be overwritten. The destination file parent directory must have been created or the copy will fail. The input stream will be closed by this function.
      Parameters:
      is - the input stream to read
      dst - the destination file
      Throws:
      IOException - if the input stream couldn't be read or if the destination file couldn't be written
    • copy

      public static void copy(InputStream input, OutputStream output) throws IOException
      Copies the data from an input stream to an output stream. Both the streams will be closed when this function returns.
      Parameters:
      input - the input stream to read
      output - the output stream to write
      Throws:
      IOException - if the input stream couldn't be read or if the output stream couldn't be written
    • delete

      public static void delete(File file) throws IOException
      Deletes a file or a directory. This function will delete all files and sub-directories inside a directory recursively.
      Parameters:
      file - the file or directory to delete
      Throws:
      IOException - if some files couldn't be deleted
      See Also:
    • deleteFiles

      public static void deleteFiles(File dir) throws IOException
      Deletes all files in a directory, but leaving the directory otherwise unmodified. This function will delete any sub-directories recursively.
      Parameters:
      dir - the directory to clean
      Throws:
      IOException - if some files couldn't be deleted
      See Also:
    • deleteEmptyDirs

      public static void deleteEmptyDirs(File dir) throws IOException
      Deletes all empty directories in a directory, but leaves the directory itself unmodified. This method will remove any empty directories recursively, making it possible to remove a tree of empty directories.
      Parameters:
      dir - the directory to clean
      Throws:
      IOException - if some files couldn't be deleted
      See Also:
    • setTempDir

      public static void setTempDir(File dir)
      Sets the temporary directory to use.
      Parameters:
      dir - the new temporary directory
    • tempFile

      public static File tempFile(String name) throws IOException
      Creates a new temporary file. If a temporary directory has been set, it will be used. Otherwise the default temporary directory will be used. The generated file is guaranteed to keep the same file extension as the desired file name.
      Parameters:
      name - the desired file name
      Returns:
      a new empty temporary file
      Throws:
      IOException - if the temporary file couldn't be created