Class FileUtils

java.lang.Object
de.iip_ecosphere.platform.support.FileUtils

public class FileUtils extends Object
Basic file functionality.
Author:
Holger Eichelberger, SSE
  • Constructor Details

    • FileUtils

      private FileUtils()
      Preventing external creation.
  • Method Details

    • createTmpFolder

      public static File createTmpFolder(String name)
      Creates a temporary folder in java.io.tmpdir without cleanup.
      Parameters:
      name - the name of the temporary folder within the system/user temporary directory
      Returns:
      the temporary folder (descriptor)
    • createTmpFolder

      public static File createTmpFolder(String name, boolean cleanup)
      Creates a temporary folder in java.io.tmpdir.
      Parameters:
      name - the name of the temporary folder within the system/user temporary directory
      cleanup - try to do an auto cleanup at JVM shutdown
      Returns:
      the temporary folder (descriptor), may receive a suffix if there is already a folder that cannot be deleted
    • deleteQuietly

      public static boolean deleteQuietly(File file)
      Deletes a file or directory, not throwing an exception. If file is a directory, delete it and all sub-directories. [convenience]
      Parameters:
      file - file or directory to delete, may be null
      Returns:
      true if file was deleted, otherwise false
    • cleanDirectory

      public static void cleanDirectory(File directory) throws IOException
      Recursively deletes all files and subdirectories within a given directory.
      Parameters:
      directory - The directory to clean.
      Throws:
      IOException - if deleting/cleaning fails
    • forceDelete

      public static void forceDelete(File file) throws IOException
      Deletes a file or directory. For a directory, delete it and all subdirectories.
      Parameters:
      file - file or directory to delete
      Throws:
      FileNotFoundException - if the file was not found
      IOException - in case deletion is unsuccessful
    • deleteOnExit

      public static void deleteOnExit(File file)
      Deletes a file or directory on JVM exit, not throwing an exception. If file is a directory, delete it and all sub-directories. [convenience]
      Parameters:
      file - file or directory to delete, may be null
    • getTempDirectoryPath

      public static String getTempDirectoryPath()
      Returns the path to the system temporary directory. [convenience]
      Returns:
      the path to the system temporary directory.
    • getTempDirectory

      public static File getTempDirectory()
      Returns a File representing the system temporary directory. [convenience]
      Returns:
      the system temporary directory.
    • getUserDirectory

      public static File getUserDirectory()
      Returns a File representing the user's home directory.
      Returns:
      the user's home directory.
    • getUserDirectoryPath

      public static String getUserDirectoryPath()
      Returns the path to the user's home directory.
      Returns:
      the path to the user's home directory.
    • closeQuietly

      public static void closeQuietly(Closeable closable)
      Closes a closable quietly.
      Parameters:
      closable - the closable, may be null
    • listFiles

      public static void listFiles(File file, Predicate<File> accept, Consumer<File> handle)
      Lists contained files.
      Parameters:
      file - the file/folder to list
      accept - accept the file for further (nested) listing, not called for folders
      handle - handle an accepted file
    • getResolvedPath

      public static String getResolvedPath(File path, String name)
      Composes a path and returns the canonical/absolute path (single-time use).
      Parameters:
      path - the path
      name - the name to be added (may be a path, may be empty or null)
      Returns:
      the canonical/absolute path
    • getResolvedFile

      public static File getResolvedFile(File path)
      Resolves a path and returns the canonical/absolute path.
      Parameters:
      path - the path
      Returns:
      the canonical/absolute path
    • fileToBase64

      public static String fileToBase64(File file) throws IOException
      Turn file into a base64 encoded string.
      Parameters:
      file - the file
      Returns:
      the base64 encoded string
      Throws:
      IOException - if file cannot be read
    • base64ToFile

      public static void base64ToFile(String string, File file) throws IOException
      Turn base64 encoded string into a file.
      Parameters:
      string - the base64 encoded string
      file - the file
      Throws:
      IOException - if file cannot be written
    • sanitizeFileName

      public static String sanitizeFileName(String str)
      Turns an arbitrary string into something that can be used as a file name.
      Parameters:
      str - the string to use
      Returns:
      the file name
    • sanitizeFileName

      public static String sanitizeFileName(String str, boolean addTimestamp)
      Turns an arbitrary string into something that can be used as a file name.
      Parameters:
      str - the string to use
      addTimestamp - whether the current timestamp shall be added to str
      Returns:
      the file name
      See Also:
    • findFile

      public static File findFile(File folder, String name)
      Tries to find the file name in folder and its subfolders.
      Parameters:
      folder - the folder to search
      name - the file name to find
      Returns:
      the found file or null for none
    • getSystemRoot

      public static File getSystemRoot()
      Returns the system root folder.
      Returns:
      the system root
    • write

      public static void write(File file, CharSequence data) throws IOException
      Writes a CharSequence with platform default charset 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:
      2.3
    • write

      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 requested charset, null means platform default
      Throws:
      IOException - in case of an I/O error
    • readFileToString

      public static String readFileToString(File file) throws IOException
      Reads the contents of a file into a String with platform default charset. The file is always closed.
      Parameters:
      file - the file to read
      Returns:
      the file contents
      Throws:
      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.
    • readFileToString

      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
      charset - the requested charset, null means platform default
      Returns:
      the file contents
      Throws:
      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.
    • deleteDirectory

      public static void deleteDirectory(File directory) throws IOException
      Deletes a directory recursively.
      Parameters:
      directory - directory to delete
      Throws:
      IOException - in case deletion is unsuccessful
      IllegalArgumentException - if directory is not a directory
    • copyDirectory

      public static void copyDirectory(File srcDir, File destDir, FileFilter filter) throws IOException
      Copies a filtered directory to a new location preserving the file dates. This method copies the contents of the specified source directory to within the specified destination directory. The destination directory is created if it does not exist. If the destination directory does exist, then this method merges the source with the destination, with the source taking precedence.
      Parameters:
      srcDir - an existing directory to copy
      destDir - the new directory
      filter - the filter to apply, null means copy all directories and files should be the same as the original
      Throws:
      IllegalArgumentException - if srcDir exists but is not a directory, or the source and the destination directory are the same
      FileNotFoundException - if the source does not exist
      IOException - if an error occurs, the destination is not writable, or setting the last-modified time didn't succeed
    • copyDirectory

      public static void copyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate) throws IOException
      Copies a filtered directory to a new location. This method copies the contents of the specified source directory to within the specified destination directory. The destination directory is created if it does not exist. If the destination directory does exist, then this method merges the source with the destination, with the source taking precedence.
      Parameters:
      srcDir - an existing directory to copy
      destDir - the new directory
      filter - the filter to apply, null means copy all directories and files
      preserveFileDate - true if the file date of the copy should be the same as the original
      Throws:
      IllegalArgumentException - if srcDir exists but is not a directory, the source and the destination directory are the same, or the destination is not writable
      FileNotFoundException - if the source does not exist
      IOException - if an error occurs or setting the last-modified time didn't succeed
    • copyDirectory

      public static void copyDirectory(File srcDir, File destDir) throws IOException
      Copies a whole directory to a new location, preserving the file dates. This method copies the specified directory and all its child directories and files to the specified destination. The destination is the new location and name of the directory. That is, copying /home/bar to /tmp/bang copies the contents of /home/bar into /tmp/bang. It does not create /tmp/bang/bar. The destination directory is created if it does not exist. If the destination directory does exist, then this method merges the source with the destination, with the source taking precedence.
      Parameters:
      srcDir - an existing directory to copy
      destDir - the new directory
      Throws:
      IllegalArgumentException - if srcDir exists but is not a directory, the source and the destination directory are the same
      FileNotFoundException - if the source does not exist.
      IOException - if an error occurs, the destination is not writable, or setting the last-modified time didn't succeed
    • copyFile

      public static void copyFile(File srcFile, File destFile) throws IOException
      Copies a file to a new location preserving the file date. This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method overwrites it. A symbolic link is resolved before copying so the new file is not a link.
      Parameters:
      srcFile - an existing file to copy
      destFile - the new file
      Throws:
      IOException - if source or destination is invalid, if an error occurs or setting the last-modified time didn't succeed, if the output file length is not the same as the input file length after the copy completes
    • writeStringToFile

      public static void writeStringToFile(File file, String data) throws IOException
      Writes a String with platform default charset to a file creating the file if it does not exist. The parent directories of the file will be created if they do 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
      UnsupportedEncodingException - if the encoding is not supported by the VM
    • writeStringToFile

      public static void writeStringToFile(File file, String data, Charset charset) throws IOException
      Writes a String to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.
      Parameters:
      file - the file to write
      data - the content to write to the file
      charset - the charset to use, null means platform default
      Throws:
      IOException - in case of an I/O error
      UnsupportedEncodingException - if the encoding is not supported by the VM
    • readFileToByteArray

      public static byte[] readFileToByteArray(File file) throws IOException
      Reads the contents of a file into a byte array. The file is always closed.
      Parameters:
      file - the file to read
      Returns:
      the file contents
      Throws:
      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.
    • writeByteArrayToFile

      public static void writeByteArrayToFile(File file, byte[] data) throws IOException
      Writes a byte array to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.
      Parameters:
      file - the file to write to
      data - the content to write to the file
      Throws:
      IOException - in case of an I/O error
    • writeByteArrayToFile

      public static void writeByteArrayToFile(File file, byte[] data, boolean append) throws IOException
      Writes a byte array to a file creating the file if it does not exist.
      Parameters:
      file - the file to write to
      data - the content to write to the file
      append - if true, then bytes will be added to the end of the file rather than overwriting
      Throws:
      IOException - in case of an I/O error
    • copyInputStreamToFile

      public static void copyInputStreamToFile(InputStream source, File destination) throws IOException
      Copies bytes from an InputStream source to a file destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.
      Parameters:
      source - the InputStream to copy bytes from, must not be, will be closed
      destination - the non-directory File to write bytes to (possibly overwriting)
      Throws:
      IOException - if destination is a directory, if destination cannot be written, if destination needs creating but can't be, if an IO error occurs during copying
    • contentEquals

      public static boolean contentEquals(File file1, File file2) throws IOException
      Tests whether the contents of two files are equal. This method checks to see if the two files are different lengths or if they point to the same file, before resorting to byte-by-byte comparison of the contents.
      Parameters:
      file1 - the first file
      file2 - the second file
      Returns:
      true if the content of the files are equal or they both don't exist, false otherwise
      Throws:
      IllegalArgumentException - when an input is not a file.
      IOException - If an I/O error occurs.