Package rife.tools

Class FileUtils

java.lang.Object
rife.tools.FileUtils

public final class FileUtils extends Object
A utility class for handling files.
Since:
1.0
  • Field Details

    • JAVA_FILE_PATTERN

      public static final Pattern JAVA_FILE_PATTERN
    • JAR_FILE_PATTERN

      public static final Pattern JAR_FILE_PATTERN
  • Method Details

    • getFileList

      public static List<String> getFileList(File dir)
      Returns a list of files in the given directory.
      Parameters:
      dir - The directory to search for files.
      Returns:
      A list of files in the given directory.
      Since:
      1.0
    • getFileList

      public static List<String> getFileList(File dir, Pattern included, Pattern excluded)
      Returns a list of files in the given directory that match the specified inclusion and exclusion patterns.
      Parameters:
      dir - The directory to search for files.
      included - A pattern for files to include.
      excluded - A pattern for files to exclude.
      Returns:
      A list of files in the given directory that match the specified inclusion and exclusion patterns.
      Since:
      1.0
    • getFileList

      public static List<String> getFileList(File dir, List<Pattern> included, List<Pattern> excluded)
      Returns a list of files in the given directory that match the specified inclusion and exclusion patterns.
      Parameters:
      dir - The directory to search for files.
      included - A list of patterns for files to include.
      excluded - A list of patterns for files to exclude.
      Returns:
      A list of files in the given directory that match the specified inclusion and exclusion patterns.
      Since:
      1.0
    • getFileList

      public static List<String> getFileList(File dir, Pattern[] included, Pattern[] excluded)
      Returns a list of files in the given directory that match the specified inclusion and exclusion patterns.
      Parameters:
      dir - The directory to search for files.
      included - An array of patterns for files to include.
      excluded - An array of patterns for files to exclude.
      Returns:
      A list of files in the given directory that match the specified inclusion and exclusion patterns.
      Since:
      1.0
    • moveFile

      public static void moveFile(File source, File target) throws FileUtilsErrorException
      Moves the source file to the target location.
      Parameters:
      source - the file to be moved.
      target - the new location to move the file to.
      Throws:
      FileUtilsErrorException - if an error occurs while moving the file.
      IllegalArgumentException - if either the source or target is null.
      Since:
      1.0
    • moveDirectory

      public static void moveDirectory(File source, File target) throws FileUtilsErrorException
      Moves the source directory to the target location.
      Parameters:
      source - the directory to be moved.
      target - the new location to move the directory to.
      Throws:
      FileUtilsErrorException - if an error occurs while moving the directory.
      IllegalArgumentException - if either the source or target is null.
      Since:
      1.0
    • deleteDirectory

      public static void deleteDirectory(File source) throws FileUtilsErrorException
      Deletes a directory and all its contents recursively.
      Parameters:
      source - the directory to be deleted.
      Throws:
      FileUtilsErrorException - if an error occurs while deleting the directory.
      IllegalArgumentException - if the source is null.
      Since:
      1.0
    • copy

      public static void copy(InputStream inputStream, OutputStream outputStream) throws FileUtilsErrorException
      Copy the contents of an InputStream to an OutputStream.
      Parameters:
      inputStream - the InputStream to copy from. Must not be null.
      outputStream - the OutputStream to copy to. Must not be null.
      Throws:
      FileUtilsErrorException - if there is an error during the copying of streams.
      IllegalArgumentException - if either inputStream or outputStream is null.
      Since:
      1.0
    • copy

      public static void copy(InputStream inputStream, File target) throws FileUtilsErrorException
      Copy the contents of an InputStream to a file.
      Parameters:
      inputStream - the InputStream to copy from; must not be null
      target - the File to copy to; must not be null
      Throws:
      FileUtilsErrorException - if an error occurs while copying the stream to the file
      IllegalArgumentException - if inputStream or target is null
      Since:
      1.0
    • copy

      public static void copy(File source, OutputStream outputStream) throws FileUtilsErrorException
      Copy the contents of a file to an OutputStream.
      Parameters:
      source - the File to copy from; must not be null
      outputStream - the OutputStream to copy to; must not be null
      Throws:
      FileUtilsErrorException - if an error occurs while copying the file to the stream
      IllegalArgumentException - if source or outputStream is null
      Since:
      1.0
    • copy

      public static void copy(File source, File target) throws FileUtilsErrorException
      Copy the contents of one file to another file.
      Parameters:
      source - the File to copy from; must not be null
      target - the File to copy to; must not be null
      Throws:
      FileUtilsErrorException - if an error occurs while copying the source file to the target file
      IllegalArgumentException - if source or target is null
      Since:
      1.0
    • copyDirectory

      public static void copyDirectory(File sourceDir, File targetDir) throws FileUtilsErrorException
      Copies all files and directories from the source directory to the target directory.
      Parameters:
      sourceDir - the directory to be copied from
      targetDir - the directory to be copied to
      Throws:
      FileUtilsErrorException - if there's an error copying the files
      IllegalArgumentException - if either sourceDir or targetDir are null
      Since:
      1.0
    • readStream

      public static ByteArrayOutputStream readStream(InputStream inputStream) throws FileUtilsErrorException
      Reads the complete contents of an input stream and returns it as a ByteArrayOutputStream object.
      Parameters:
      inputStream - the input stream to be read.
      Returns:
      a ByteArrayOutputStream object containing the complete contents of the specified input stream.
      Throws:
      FileUtilsErrorException - if there is an error while reading the input stream.
      IllegalArgumentException - if the inputStream parameter is null.
      Since:
      1.0
    • readString

      public static String readString(InputStream inputStream) throws FileUtilsErrorException
      Reads the entire contents of an InputStream and returns it as a String, using UTF-8 encoding.
      Parameters:
      inputStream - The InputStream to read the contents from.
      Returns:
      A String containing the complete contents of the InputStream.
      Throws:
      FileUtilsErrorException - If there was an error while reading the InputStream.
      IllegalArgumentException - If the inputStream is null.
      Since:
      1.0
    • readString

      public static String readString(Reader reader) throws FileUtilsErrorException
      Reads the entire contents of a Reader and returns it as a String.
      Parameters:
      reader - The Reader to read the contents from.
      Returns:
      A String containing the complete contents of the Reader.
      Throws:
      FileUtilsErrorException - If there was an error while reading the Reader.
      IllegalArgumentException - If the reader is null.
      Since:
      1.0
    • readString

      public static String readString(InputStream inputStream, String encoding) throws FileUtilsErrorException
      Reads the entire contents of an InputStream and returns it as a String, using the specified character encoding.
      Parameters:
      inputStream - The InputStream to read the contents from.
      encoding - The name of the character encoding to use.
      Returns:
      A String containing the complete contents of the InputStream.
      Throws:
      FileUtilsErrorException - If there was an error while reading the InputStream.
      IllegalArgumentException - If the inputStream is null.
      Since:
      1.0
    • readString

      public static String readString(URL source, String encoding) throws FileUtilsErrorException
      Reads the content of the given URL into a string.
      Parameters:
      source - the URL to read from
      encoding - the character encoding to use, or null to use the default encoding
      Returns:
      the content of the URL as a string
      Throws:
      FileUtilsErrorException - if an error occurs while reading the URL
      IllegalArgumentException - if source is null
      Since:
      1.0
    • readString

      public static String readString(File source) throws FileUtilsErrorException
      Reads the content of the given file into a string using the default encoding.
      Parameters:
      source - the file to read from
      Returns:
      the content of the file as a string
      Throws:
      FileUtilsErrorException - if an error occurs while reading the file
      IllegalArgumentException - if source is null
      Since:
      1.0
    • readString

      public static String readString(File source, String encoding) throws FileUtilsErrorException
      Reads the content of the given file into a string using the specified character encoding.
      Parameters:
      source - the file to read from
      encoding - the character encoding to use, or null to use the default encoding
      Returns:
      the content of the file as a string
      Throws:
      FileUtilsErrorException - if an error occurs while reading the file
      IllegalArgumentException - if source is null
      Since:
      1.0
    • readString

      public static String readString(URL source) throws FileUtilsErrorException
      Reads all characters from the URL and returns its contents as a string.
      Parameters:
      source - the URL to read from
      Returns:
      a string containing all the characters read from the URL
      Throws:
      FileUtilsErrorException - if an error occurs while reading the URL
      IllegalArgumentException - if source is null
      Since:
      1.0
    • readBytes

      public static byte[] readBytes(InputStream inputStream) throws FileUtilsErrorException
      Reads all the bytes from the InputStream.
      Parameters:
      inputStream - the inputStream to read from
      Returns:
      a byte array containing all the bytes read from the input stream
      Throws:
      FileUtilsErrorException - if an error occurs while reading the input stream
      IllegalArgumentException - if inputStream is null
      Since:
      1.0
    • readBytes

      public static byte[] readBytes(URL source) throws FileUtilsErrorException
      Reads all the bytes from the file denoted by the specified File object.
      Parameters:
      source - the file to read from
      Returns:
      a byte array containing all the bytes read from the file
      Throws:
      FileUtilsErrorException - if an error occurs while reading the file
      IllegalArgumentException - if source is null
      Since:
      1.0
    • readBytes

      public static byte[] readBytes(File source) throws FileUtilsErrorException
      Reads all the bytes from the URL.
      Parameters:
      source - the URL to read from
      Returns:
      a byte array containing all the bytes read from the URL
      Throws:
      FileUtilsErrorException - if an error occurs while reading the URL
      IllegalArgumentException - if source is null
      Since:
      1.0
    • writeBytes

      public static void writeBytes(byte[] content, File destination) throws FileUtilsErrorException
      Writes a byte array to a given file.
      Parameters:
      content - the byte array to be written
      destination - the file to which the byte array should be written
      Throws:
      FileUtilsErrorException - if there is an error writing the byte array to the file
      IllegalArgumentException - if either the content or destination arguments are null
      Since:
      1.0
    • writeString

      public static void writeString(String content, File destination) throws FileUtilsErrorException
      Writes a string to a given file.
      Parameters:
      content - the string to be written
      destination - the file to which the string should be written
      Throws:
      FileUtilsErrorException - if there is an error writing the string to the file
      IllegalArgumentException - if either the content or destination arguments are null
      Since:
      1.0
    • deleteFile

      public static void deleteFile(File file)
      Deletes a given file.
      Parameters:
      file - The file to be deleted.
      Throws:
      IllegalArgumentException - if file is null.
      Since:
      1.0
    • getUniqueFilename

      public static String getUniqueFilename()
      Generates a unique filename.
      Returns:
      A unique filename string.
      Since:
      1.0
    • unzipFile

      public static void unzipFile(File source, File destination) throws FileUtilsErrorException
      Unzips a file from the source location to a destination.
      Parameters:
      source - the file to unzip (must not be null)
      destination - the location to unzip the file to (must not be null)
      Throws:
      FileUtilsErrorException - if an error occurs while unzipping the file or creating directories
      Since:
      1.0
    • getBaseName

      public static String getBaseName(File file)
      Returns the base name of the specified file, without the extension.
      Parameters:
      file - The file whose base name is to be returned.
      Returns:
      The base name of the file.
      Since:
      1.0
    • getBaseName

      public static String getBaseName(String fileName)
      Returns the base name of the specified file name, without the extension.
      Parameters:
      fileName - The name of the file whose base name is to be returned.
      Returns:
      The base name of the file.
      Throws:
      IllegalArgumentException - if fileName is null.
      Since:
      1.0
    • getExtension

      public static String getExtension(File file)
      Returns the extension of the specified File.
      Parameters:
      file - the file to get the extension from
      Returns:
      the extension of the given file or null if the file has no extension.
      Throws:
      IllegalArgumentException - if file is null
      Since:
      1.0
    • getExtension

      public static String getExtension(String fileName)
      Returns the extension of the specified file name.
      Parameters:
      fileName - the name of the file to get the extension from
      Returns:
      the extension of the given file name or null if the file name has no extension.
      Throws:
      IllegalArgumentException - if fileName is null
      Since:
      1.0
    • combineToAbsolutePaths

      @SafeVarargs public static List<String> combineToAbsolutePaths(List<File>... files)
      Combines an arbitrary number of lists of files into a single list of absolute file paths.
      Parameters:
      files - zero or more lists of files to be combined
      Returns:
      the list of absolute file paths resulting from combining the input lists
      Since:
      1.5.2
    • generateDirectoryListing

      public static String generateDirectoryListing(File directory) throws IOException
      Generates a sorted directory listing of all files and subdirectories in the specified directory.

      This will be formatted for Posix use.

      Parameters:
      directory - The root directory to list.
      Returns:
      A string containing a sorted directory listing of all files and subdirectories in the specified directory.
      Throws:
      IOException - If an error occurs while listing the directory.
      Since:
      1.5.0
    • joinPaths

      public static String joinPaths(List<String> paths)
      Joins a list of file paths into a single string with the file path separator.
      Parameters:
      paths - A list of file paths to join.
      Returns:
      A single string consisting of all the input file paths separated by the file path separator, or an empty string if input list is null.
      Since:
      1.5.2
    • getJavaFileList

      public static List<File> getJavaFileList(File directory)
      Returns a list of Java files in the specified directory.
      Parameters:
      directory - The directory to search for Java files.
      Returns:
      A list of File objects pointing to the Java files in the directory, or an empty list if input directory is null.
      Since:
      1.5.10
    • transformFiles

      public static void transformFiles(File directory, Function<String,String> transformer) throws FileUtilsErrorException
      Transforms all files in the specified directory using the provided transformation function.
      Parameters:
      directory - The directory containing the files to be transformed.
      transformer - The transformation function to be applied to each file.
      Throws:
      FileUtilsErrorException - If there is an error while reading or writing a file.
      Since:
      1.5.10
    • transformFiles

      public static void transformFiles(File directory, Pattern included, Pattern excluded, Function<String,String> transformer) throws FileUtilsErrorException
      Transforms files in the specified directory that match the included pattern and do not match the excluded pattern using the provided transformation function.
      Parameters:
      directory - The directory containing the files to be transformed.
      included - A regex pattern specifying which files to include.
      excluded - A regex pattern specifying which files to exclude.
      transformer - The transformation function to be applied to each file.
      Throws:
      FileUtilsErrorException - If there is an error while reading or writing a file.
      Since:
      1.5.10
    • transformFiles

      public static void transformFiles(File directory, List<Pattern> included, List<Pattern> excluded, Function<String,String> transformer) throws FileUtilsErrorException
      Transforms files in the specified directory that match at least one of the included patterns and do not match any of the excluded patterns using the provided transformation function.
      Parameters:
      directory - The directory containing the files to be transformed.
      included - A list of regex patterns specifying which files to include.
      excluded - A list of regex patterns specifying which files to exclude.
      transformer - The transformation function to be applied to each file.
      Throws:
      FileUtilsErrorException - If there is an error while reading or writing a file.
      Since:
      1.5.10
    • transformFiles

      public static void transformFiles(File directory, Pattern[] included, Pattern[] excluded, Function<String,String> transformer) throws FileUtilsErrorException
      Transforms files in the specified directory that match at least one of the included patterns and do not match any of the excluded patterns using the provided transformation function.
      Parameters:
      directory - The directory containing the files to be transformed.
      included - An array of regex patterns specifying which files to include.
      excluded - An array of regex patterns specifying which files to exclude.
      transformer - The transformation function to be applied to each file.
      Throws:
      FileUtilsErrorException - If there is an error while reading or writing a file.
      Since:
      1.5.10
    • permissionsFromMode

      public static Set<PosixFilePermission> permissionsFromMode(int mode)
      Creates a new set of PosixFilePermission based on the given Posix mode.

      Standard Posix permissions can be provided on octal numbers, for instance 0755 and 0644.

      Parameters:
      mode - an integer containing permissions for the owner, group and others
      Returns:
      a Set of PosixFilePermission
      Since:
      1.5.19
    • path

      public static Path path(File file, String... paths)
      Constructs a new Path instance using the specified file and additional path elements.
      Parameters:
      file - The file to use as the starting point for the Path.
      paths - Additional path elements to add to the Path.
      Returns:
      A new Path instance.
      Since:
      1.5.19
    • path

      public static Path path(Path path, String... paths)
      Constructs a new Path instance using the specified file and additional path elements.
      Parameters:
      path - The path to use as the starting point for the Path.
      paths - Additional path elements to add to the Path.
      Returns:
      A new Path instance.
      Since:
      1.5.19
    • versionFromResource

      public static String versionFromResource(String name)
      Reads the contents of a resource for use as a version number.
      Parameters:
      name - the name of the resource to look for
      Returns:
      the version number; or "unknown version" if the version resource couldn't be found
      Since:
      1.7