Package adams.core.io

Class FileUtils


  • public class FileUtils
    extends Object
    Utility class for I/O related actions.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    • Field Detail

      • m_Properties

        protected static Properties m_Properties
        the properties.
      • FILENAME_CHARS

        protected static String FILENAME_CHARS
        valid characters for filenames.
      • MAX_EXTENSION_LENGTH

        protected static Integer MAX_EXTENSION_LENGTH
        the maximum length for an extension.
      • IGNORED_EXTENSION_SUFFIXES

        protected static String[] IGNORED_EXTENSION_SUFFIXES
        the ignored extension suffixes.
      • BINARY_CHECK_BUFFER_SIZE

        public static final int BINARY_CHECK_BUFFER_SIZE
        the length of the buffer for binary checks.
        See Also:
        Constant Field Values
    • Constructor Detail

      • FileUtils

        public FileUtils()
    • Method Detail

      • getProperties

        protected static Properties getProperties()
        Returns the properties.
        Returns:
        the properties
      • getFileNameChars

        public static String getFileNameChars()
        Returns the characters that can be used in filenames.
        Returns:
        the characters (in single string)
      • getMaxExtensionLength

        public static int getMaxExtensionLength()
        Returns the maximum length of extensions.
        Returns:
        the max length
      • getIgnoredExtensionSuffixes

        public static String[] getIgnoredExtensionSuffixes()
        Returns the extension suffixes that get ignored in determining a file's extension. Examples are "gz" or "bz2".
        Returns:
        the suffixes
      • removeUTF8BOM

        public static void removeUTF8BOM​(StringBuilder s)
        Removes a UTF-8 BOM.
        Parameters:
        s - the current string
      • removeUTF8BOM

        public static String removeUTF8BOM​(String s)
        Removes a UTF-8 BOM.
        Parameters:
        s - the string to process
        Returns:
        the (potentially) updated string
      • removeByteOrderMarks

        public static String removeByteOrderMarks​(String s)
        Removes byte order marks (BOMs) from the start of the string (if present). For UTF-16 and UTF-32.
        Parameters:
        s - the string to process
        Returns:
        the processed string
      • removeAllByteOrderMarks

        public static String removeAllByteOrderMarks​(String s)
        Removes byte order marks (BOMs) from the start of the string (if present). For UTDF-8, UTF-16 and UTF-32.
        Parameters:
        s - the string to process
        Returns:
        the processed string
      • loadFromFile

        public static List<String> loadFromFile​(File file)
        Returns the content of the given file, null in case of an error. Also handles gzip compressed files (uses newline to split into lines).
        Parameters:
        file - the file to load
        Returns:
        the content/lines of the file
      • loadFromFile

        public static List<String> loadFromFile​(File file,
                                                String encoding)
        Returns the content of the given file, null in case of an error. Also handles gzip compressed files (uses newline to split into lines).
        Parameters:
        file - the file to load
        encoding - the encoding to use, null to use default
        Returns:
        the content/lines of the file, null in case of an error
      • loadFromFile

        public static List<String> loadFromFile​(File file,
                                                String encoding,
                                                boolean handleGzip)
        Returns the content of the given file, null in case of an error. Also handles gzip compressed files (uses newline to split into lines).
        Parameters:
        file - the file to load
        encoding - the encoding to use, null to use default
        Returns:
        the content/lines of the file, null in case of an error
      • loadFromBinaryFile

        public static byte[] loadFromBinaryFile​(File file)
        Loads the binary file.
        Parameters:
        file - the file to load
        Returns:
        the binary content, null in case of an error
      • loadFromBinaryFile

        public static byte[] loadFromBinaryFile​(File file,
                                                int max)
        Loads the specified maximum number of bytes from a binary file.
        Parameters:
        file - the file to load
        max - the maximum number of bytes to load
        Returns:
        the binary content, null in case of an error
      • loadHexFromBinaryFile

        public static String loadHexFromBinaryFile​(File file)
        Loads the binary file and returns the hexadecimal representation. Uses 16 columns.
        Parameters:
        file - the file to load
        Returns:
        the binary content as hex, null in case of an error
      • loadHexFromBinaryFile

        public static String loadHexFromBinaryFile​(File file,
                                                   int columns)
        Loads the binary file and returns the hexadecimal representation.
        Parameters:
        file - the file to load
        columns - the number of columns to generate
        Returns:
        the binary content as hex, null in case of an error
      • saveToFile

        public static boolean saveToFile​(String[] content,
                                         File file)
        Saves the content to the given file.
        Parameters:
        content - the content to save
        file - the file to save the content to
        Returns:
        true if successfully saved
      • saveToFile

        public static boolean saveToFile​(List<String> content,
                                         File file)
        Saves the content to the given file.
        Parameters:
        content - the content to save
        file - the file to save the content to
        Returns:
        true if successfully saved
      • saveToFile

        public static boolean saveToFile​(List<String> content,
                                         File file,
                                         String encoding)
        Saves the content to the given file.
        Parameters:
        content - the content to save
        file - the file to save the content to
        encoding - the encoding to use, null for default
        Returns:
        true if successfully saved
      • saveToFileMsg

        public static String saveToFileMsg​(List<String> content,
                                           File file,
                                           String encoding)
        Saves the content to the given file.
        Parameters:
        content - the content to save
        file - the file to save the content to
        encoding - the encoding to use, null for default
        Returns:
        true if successfully saved
      • writeToBinaryFile

        public static boolean writeToBinaryFile​(String filename,
                                                byte[] data)
        Writes the binary data to the specified file. The file is always overwritten.
        Parameters:
        filename - the file to write to
        data - the data to write
        Returns:
        true if writing was successful
      • writeToBinaryFile

        public static boolean writeToBinaryFile​(String filename,
                                                byte[] data,
                                                boolean append)
        Writes the binary data to the specified file.
        Parameters:
        filename - the file to write to
        data - the data to write
        append - whether to append the file
        Returns:
        true if writing was successful
      • writeToFile

        public static boolean writeToFile​(String filename,
                                          Object obj)
        Writes the given object to the specified file. The object is always appended.
        Parameters:
        filename - the file to write to
        obj - the object to write
        Returns:
        true if writing was successful
      • writeToFile

        public static boolean writeToFile​(String filename,
                                          Object obj,
                                          String encoding)
        Writes the given object to the specified file. The object is always appended.
        Parameters:
        filename - the file to write to
        obj - the object to write
        encoding - the encoding to use, null for default
        Returns:
        true if writing was successful
      • writeToFile

        public static boolean writeToFile​(String filename,
                                          Object obj,
                                          boolean append)
        Writes the given object to the specified file. The message is either appended or replaces the current content of the file.
        Parameters:
        filename - the file to write to
        obj - the object to write
        append - whether to append the message or not
        Returns:
        true if writing was successful
      • writeToFile

        public static boolean writeToFile​(String filename,
                                          Object obj,
                                          boolean append,
                                          String encoding)
        Writes the given object to the specified file. The message is either appended or replaces the current content of the file.
        Parameters:
        filename - the file to write to
        obj - the object to write
        append - whether to append the message or not
        encoding - the encoding to use, null for default
        Returns:
        true if writing was successful
      • writeToFileMsg

        public static String writeToFileMsg​(String filename,
                                            Object obj,
                                            boolean append,
                                            String encoding)
        Writes the given object to the specified file. The message is either appended or replaces the current content of the file.
        Parameters:
        filename - the file to write to
        obj - the object to write
        append - whether to append the message or not
        encoding - the encoding to use, null for default
        Returns:
        true if writing was successful
      • copyOrMove

        public static boolean copyOrMove​(File sourceLocation,
                                         File targetLocation,
                                         boolean move,
                                         boolean atomic)
                                  throws IOException
        Copies or moves files and directories (recursively). If targetLocation does not exist, it will be created.

        Original code from Java-Tips.org.
        Parameters:
        sourceLocation - the source file/dir
        targetLocation - the target file/dir
        move - if true then the source files/dirs get deleted as soon as copying finished
        atomic - whether to perform an atomic move operation
        Returns:
        false if failed to delete when moving or failed to create target directory
        Throws:
        IOException - if copying/moving fails
      • copy

        public static boolean copy​(File sourceLocation,
                                   File targetLocation)
                            throws IOException
        Copies the file/directory (recursively).
        Parameters:
        sourceLocation - the source file/dir
        targetLocation - the target file/dir
        Returns:
        if successfully copied
        Throws:
        IOException - if copying fails
      • move

        public static boolean move​(File sourceLocation,
                                   File targetLocation)
                            throws IOException
        Moves the file/directory (recursively).
        Parameters:
        sourceLocation - the source file/dir
        targetLocation - the target file/dir
        Returns:
        if successfully moved
        Throws:
        IOException - if moving fails
      • move

        public static boolean move​(File sourceLocation,
                                   File targetLocation,
                                   boolean atomic)
                            throws IOException
        Moves the file/directory (recursively).
        Parameters:
        sourceLocation - the source file/dir
        targetLocation - the target file/dir
        atomic - whether to perform an atomic move operation
        Returns:
        if successfully moved
        Throws:
        IOException - if moving fails
      • delete

        public static boolean delete​(String file)
        Deletes the specified file. If the file represents a directory, then this will get deleted recursively.
        Parameters:
        file - the file/dir to delete
        Returns:
        true if successfully deleted
      • delete

        public static boolean delete​(File file)
        Deletes the specified file. If the file represents a directory, then this will get deleted recursively.
        Parameters:
        file - the file/dir to delete
        Returns:
        true if successfully deleted
      • createFilename

        public static String createFilename​(String s,
                                            String replace)
        Replaces all characters that would create problems on a filesystem. The string is to be expected a filename without a path.
        Parameters:
        s - the string to process
        replace - the character to replace "invalid" characters with, use empty string to strip "invalid" characters instead of replacing them.
        Returns:
        the processed string
      • getDirectoryDepth

        public static int getDirectoryDepth​(File file)
        Returns the number of directories that this file object contains. E.g.: /home/blah/some/where.txt will return 3. /blah.txt returns 0.
        Parameters:
        file - the file
      • createPartialFilename

        public static String createPartialFilename​(File file,
                                                   int numParentDirs)
        Creates a partial filename for the given file, based on how many parent directories should be included. Examples:
         createPartialFilename(new File("/home/some/where/file.txt"), -1)
           = /home/some/where/file.txt
         createPartialFilename(new File("/home/some/where/file.txt"), 0)
           = file.txt
         createPartialFilename(new File("/home/some/where/file.txt"), 1)
           = where/file.txt
         createPartialFilename(new File("/home/some/where/file.txt"), 2)
           = some/where/file.txt
         
        Parameters:
        file - the file to create the partial filename for
        numParentDirs - the number of parent directories to include in the partial name, -1 returns the absolute filename
        Returns:
        the generated filename
      • isDirEmpty

        public static boolean isDirEmpty​(File dir)
        Checks whether the directory is empty.
        Parameters:
        dir - the directory to check
        Returns:
        true if empty
      • isDirEmpty

        public static boolean isDirEmpty​(String dir)
        Checks whether the directory is empty.
        Parameters:
        dir - the directory to check
        Returns:
        true if empty
      • isDirEmpty

        public static boolean isDirEmpty​(File dir,
                                         String regExp)
        Checks whether the directory is empty.
        Parameters:
        dir - the directory to check
        regExp - a regular expression to look for, use null to ignore
        Returns:
        true if empty
      • isDirEmpty

        public static boolean isDirEmpty​(String dir,
                                         String regExp)
        Checks whether the directory is empty.
        Parameters:
        dir - the directory to check
        regExp - a regular expression to look for, use null to ignore
        Returns:
        true if empty or directory does not exist
      • fixExecutable

        public static String fixExecutable​(String executable)
        Adjusts the extension according to the platform. For Windows it automatically adds ".exe" it neither ".com" nor ".exe" extension present. For other platforms it removes ".exe" and ".com".
        Parameters:
        executable - the executable (full path or just filename) to process
        Returns:
        the processed executable
      • quoteExecutable

        public static String quoteExecutable​(String executable)
        Surrounds the executable with double quotes if a blank is in the path.
        Parameters:
        executable - the executable (full path, no parameters)
        Returns:
        the processed executable
      • removeIgnoredExtensionSuffixes

        public static String removeIgnoredExtensionSuffixes​(String filename)
        Removes any ignored extension suffixes from the filename.
        Parameters:
        filename - the filename to process
        Returns:
        the processed filename
      • getExtension

        public static String getExtension​(File file)
        Returns the extension of the file, if any.
        Parameters:
        file - the file to get the extension from
        Returns:
        the extension (no dot), null if none available
      • getExtension

        public static String getExtension​(String filename)
        Returns the extension of the file, if any.
        Parameters:
        filename - the file to get the extension from
        Returns:
        the extension (no dot), null if none available
      • getExtensions

        public static String[] getExtensions​(File file)
        Returns the extensions of the file, if any. Returns "txt.gz" and "gz", for instance, for file "hello_world.txt.gz". The longer extension always comes first.
        Parameters:
        file - the file to get the extensions from
        Returns:
        the extensions (no dot), null if none available
      • getExtensions

        public static String[] getExtensions​(String filename)
        Returns the extensions of the file, if any. Removes ignored file extension suffixes like "gz" first.
        Parameters:
        filename - the file to get the extensions from
        Returns:
        the extensions (no dot), null if none available
        See Also:
        getIgnoredExtensionSuffixes()
      • replaceExtension

        public static File replaceExtension​(File file,
                                            String newExt)
        Replaces the extension of the given file with the new one. Leave the new extension empty if you want to remove the extension. Always removes ignored extension suffixes first from the filename.
        Parameters:
        file - the file to replace the extension for
        newExt - the new extension (incl dot), empty string to remove extension
        Returns:
        the updated file
        See Also:
        getIgnoredExtensionSuffixes()
      • replaceExtension

        public static PlaceholderFile replaceExtension​(PlaceholderFile file,
                                                       String newExt)
        Replaces the extension of the given file with the new one. Leave the new extension empty if you want to remove the extension. Always removes ignored extension suffixes first from the filename.
        Parameters:
        file - the file to replace the extension for
        newExt - the new extension (incl dot), empty string to remove extension
        Returns:
        the updated file
        See Also:
        getIgnoredExtensionSuffixes()
      • replaceExtension

        public static String replaceExtension​(String file,
                                              String newExt)
        Replaces the extension of the given file with the new one. Leave the new extension empty if you want to remove the extension. Always removes ignored extension suffixes first from the filename.
        Parameters:
        file - the file to replace the extension for
        newExt - the new extension (incl dot), empty string to remove extension
        Returns:
        the updated file
        See Also:
        getIgnoredExtensionSuffixes()
      • isBinary

        public static boolean isBinary​(String filename)
        Returns whether the file is (most likely) a binary one.
        Parameters:
        filename - the file to check
        Returns:
        true if a binary file
      • isBinary

        public static boolean isBinary​(File file)
        Returns whether the file is (most likely) a binary one. Reads the first kb and analyzes the bytes (skips tab, cr, lf).
        Parameters:
        file - the file to check
        Returns:
        true if a binary file
      • convertPlaceholder

        public static String convertPlaceholder​(String file)
        Ensures that the file string no longer contains a placeholder.
        Parameters:
        file - the file name to process
        Returns:
        the "purged" file name
      • toFileArray

        public static File[] toFileArray​(Object input)
        Turns String, String[], File, File[] into a File array. Ensures that strings don't contain placeholders and File objects aren't PlaceholderFile objects.
        Parameters:
        input - the input
        Returns:
        the File array
        Throws:
        IllegalArgumentException - if unsupported input class
      • toStringArray

        public static String[] toStringArray​(Object input)
        Turns String, String[], File, File[] into a String array. Ensures that strings don't contain placeholders.
        Parameters:
        input - the input
        Returns:
        the String array
        Throws:
        IllegalArgumentException - if unsupported input class
      • useForwardSlashes

        public static String useForwardSlashes​(String path)
        Converts backslashes in a path to forwards slashes. However, leaves UNC path prefix "\\" intact.
        Parameters:
        path - the path/filename to convert
        Returns:
        the path/filename with forward slashes
      • closeQuietly

        public static void closeQuietly​(InputStream is)
        Closes the stream, if possible, suppressing any exception.
        Parameters:
        is - the stream to close
      • closeQuietly

        public static void closeQuietly​(OutputStream os)
        Closes the stream, if possible, suppressing any exception.
        Parameters:
        os - the stream to close
      • closeQuietly

        public static void closeQuietly​(Reader reader)
        Closes the reader, if possible, suppressing any exception.
        Parameters:
        reader - the reader to close
      • closeQuietly

        public static void closeQuietly​(Writer writer)
        Closes the writer, if possible, suppressing any exception.
        Parameters:
        writer - the writer to close
      • closeQuietly

        public static void closeQuietly​(Closeable closeable)
        Closes the closeable object, if possible, suppressing any exception.
        Parameters:
        closeable - the closeable object to close
      • isOpen

        public static boolean isOpen​(File file)
        Checks whether the file is open/accessed by another process (Windows/*nix).
        Parameters:
        file - the file to check
        Returns:
        true if still open/accessed by another process
      • isOpenNonWindows

        public static boolean isOpenNonWindows​(File file)
        Checks whether the file is open/accessed by another process (Windows/*nix).
        Parameters:
        file - the file to check
        Returns:
        true if still open/accessed by another process
      • isOpenWindows

        public static boolean isOpenWindows​(File file)
        Checks whether the file is open/accessed by another process.
        Parameters:
        file - the file to check
        Returns:
        true if still open/accessed by another process
      • fileExists

        public static boolean fileExists​(String filename)
        Checks whether the file (not directory!) exists.
        Parameters:
        filename - the filename to check
        Returns:
        true if file exists
      • fileExists

        public static boolean fileExists​(File file)
        Checks whether the file (not directory!) exists.
        Parameters:
        file - the file to check
        Returns:
        true if file exists
      • dirExists

        public static boolean dirExists​(String dirname)
        Checks whether the directory (not file!) exists.
        Parameters:
        dirname - the directory to check
        Returns:
        true if file exists
      • dirExists

        public static boolean dirExists​(File dir)
        Checks whether the directory (not file!) exists.
        Parameters:
        dir - the directory to check
        Returns:
        true if file exists
      • dirOrParentDirExists

        public static boolean dirOrParentDirExists​(String filename)
        Checks whether the directory exists. If this file object represents a file, then the parent directory is checked.
        Parameters:
        filename - the filename to check
        Returns:
        true if directory exists
      • dirOrParentDirExists

        public static boolean dirOrParentDirExists​(File file)
        Checks whether the directory exists. If this file object represents a file, then the parent directory is checked.
        Parameters:
        file - the file object to check
        Returns:
        true if directory exists