Package jodd.io

Class FileUtil


  • public class FileUtil
    extends java.lang.Object
    File utilities.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String tempFilePrefix
      Default prefix for temporary files.
    • Constructor Summary

      Constructors 
      Constructor Description
      FileUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static void appendBytes​(java.io.File dest, byte[] data)  
      static void appendBytes​(java.io.File dest, byte[] data, int off, int len)
      Appends bytes.
      static void appendBytes​(java.lang.String dest, byte[] data)  
      static void appendBytes​(java.lang.String dest, byte[] data, int off, int len)  
      static void appendString​(java.io.File dest, java.lang.String data)  
      static void appendString​(java.io.File dest, java.lang.String data, java.nio.charset.Charset encoding)
      Appends String.
      static void appendString​(java.lang.String dest, java.lang.String data)  
      static void appendString​(java.lang.String dest, java.lang.String data, java.nio.charset.Charset encoding)  
      static void cleanDir​(java.io.File destDir)
      Cleans a directory without deleting it.
      static void cleanDir​(java.lang.String dest)  
      static boolean compare​(java.io.File one, java.io.File two)
      Compare the contents of two Files to determine if they are equal or not.
      static boolean compare​(java.lang.String file1, java.lang.String file2)  
      static void copy​(java.io.File src, java.io.File dest)
      Smart copy.
      static void copy​(java.lang.String src, java.lang.String dest)  
      static void copyDir​(java.io.File srcDir, java.io.File destDir)
      Copies directory with specified copy params.
      static void copyDir​(java.lang.String srcDir, java.lang.String destDir)  
      static void copyFile​(java.io.File srcFile, java.io.File destFile)
      Copies a File to another File.
      static void copyFile​(java.lang.String srcFile, java.lang.String destFile)  
      static java.io.File copyFileToDir​(java.io.File srcFile, java.io.File destDir)
      Copies a File to directory with specified copy params and returns copied destination.
      static java.io.File copyFileToDir​(java.lang.String srcFile, java.lang.String destDir)  
      static java.io.File createTempDirectory()  
      static java.io.File createTempDirectory​(java.lang.String prefix, java.lang.String suffix)  
      static java.io.File createTempDirectory​(java.lang.String prefix, java.lang.String suffix, java.io.File tempDir)
      Creates temporary directory.
      static java.io.File createTempFile()  
      static java.io.File createTempFile​(java.lang.String prefix, java.lang.String suffix, java.io.File tempDir)
      Creates temporary File.
      static java.io.File createTempFile​(java.lang.String prefix, java.lang.String suffix, java.io.File tempDir, boolean create)
      Creates temporary File.
      static void delete​(java.io.File dest)
      Smart delete of destination file or directory.
      static void delete​(java.lang.String dest)  
      static void deleteDir​(java.io.File destDir)
      Deletes a directory.
      static void deleteDir​(java.lang.String destDir)  
      static void deleteFile​(java.io.File destFile)
      Deletes a File.
      static void deleteFile​(java.lang.String destFile)  
      static boolean equals​(java.io.File one, java.io.File two)
      Checks if two Files point to the same File.
      static boolean equals​(java.lang.String one, java.lang.String two)  
      static java.io.File file​(java.lang.String fileName)
      Simple factory for File objects but with home resolving.
      static java.io.File getParentFile​(java.io.File file)
      Returns parent for the file.
      static boolean isAncestor​(java.io.File ancestor, java.io.File file, boolean strict)
      Check if one File is an ancestor of second one.
      static boolean isBinary​(java.io.File file)
      Checks the start of the file for ASCII control characters
      static boolean isExistingFile​(java.io.File file)
      Returns true if File exists.
      static boolean isExistingFolder​(java.io.File folder)
      Returns true if directory exists.
      static boolean isFilePathAcceptable​(java.io.File file, java.io.FileFilter fileFilter)
      Checks if file and its ancestors are acceptable by using FileFilter.accept(File).
      static boolean isNewer​(java.io.File file, long timeMillis)
      Tests if the specified File is newer than the specified time reference.
      static boolean isNewer​(java.io.File file, java.io.File reference)
      Uses File.lastModified() for reference.
      static boolean isNewer​(java.lang.String file, long timeMillis)  
      static boolean isNewer​(java.lang.String file, java.lang.String reference)  
      static boolean isOlder​(java.io.File file, long timeMillis)
      Tests if the specified File is older than the specified time reference.
      static boolean isOlder​(java.io.File file, java.io.File reference)
      Uses File.lastModified() for reference.
      static boolean isOlder​(java.lang.String file, long timeMillis)  
      static boolean isOlder​(java.lang.String file, java.lang.String reference)  
      static boolean isSymlink​(java.io.File file)
      Deprecated.
      Files.isSymbolicLink(java.nio.file.Path) provides this functionality natively as of Java 1.7.
      static java.lang.String md5​(java.io.File file)
      Creates MD5 digest of a File.
      static java.io.File mkdir​(java.io.File dir)
      Creates single directory.
      static java.io.File mkdir​(java.lang.String dir)  
      static java.io.File mkdirs​(java.io.File dirs)
      Creates all directories at once.
      static java.io.File mkdirs​(java.lang.String dirs)  
      static void move​(java.io.File src, java.io.File dest)
      Smart move.
      static void move​(java.lang.String src, java.lang.String dest)  
      static java.io.File moveDir​(java.io.File srcDir, java.io.File destDir)  
      static java.io.File moveDir​(java.lang.String srcDir, java.lang.String destDir)  
      static java.io.File moveFile​(java.io.File srcFile, java.io.File destFile)  
      static java.io.File moveFile​(java.lang.String srcFile, java.lang.String destFile)  
      static java.io.File moveFileToDir​(java.io.File srcFile, java.io.File destDir)
      Moves a file to a directory.
      static java.io.File moveFileToDir​(java.lang.String srcFile, java.lang.String destDir)  
      protected static void outBytes​(java.io.File dest, byte[] data, int off, int len, boolean append)
      Writes data to File destination.
      protected static void outChars​(java.io.File dest, char[] data, java.nio.charset.Charset encoding, boolean append)
      Writes characters to File destination.
      protected static void outString​(java.io.File dest, java.lang.String data, java.nio.charset.Charset encoding, boolean append)
      Writes data using encoding to File.
      static byte[] readBytes​(java.io.File file)  
      static byte[] readBytes​(java.io.File file, int count)
      Read file and returns byte array with contents.
      static byte[] readBytes​(java.lang.String file)  
      static char[] readChars​(java.io.File file)  
      static char[] readChars​(java.io.File file, java.nio.charset.Charset encoding)
      Reads file content as char array.
      static char[] readChars​(java.lang.String fileName)  
      static char[] readChars​(java.lang.String fileName, java.nio.charset.Charset encoding)  
      static java.lang.String[] readLines​(java.io.File source)  
      static java.lang.String[] readLines​(java.io.File file, java.nio.charset.Charset encoding)
      Reads lines from source File with specified encoding and returns lines as Strings in array.
      static java.lang.String[] readLines​(java.lang.String source)  
      static java.lang.String[] readLines​(java.lang.String source, java.nio.charset.Charset encoding)  
      static java.lang.String readString​(java.io.File source)  
      static java.lang.String readString​(java.io.File file, java.nio.charset.Charset encoding)
      Reads File content as String encoded in provided encoding.
      static java.lang.String readString​(java.lang.String source)  
      static java.lang.String readString​(java.lang.String source, java.nio.charset.Charset encoding)  
      static char[] readUTFChars​(java.io.File file)
      Reads UTF file content as char array.
      static char[] readUTFChars​(java.lang.String fileName)  
      static java.lang.String readUTFString​(java.io.File file)
      Detects optional BOM and reads UTF String from a File.
      static java.lang.String readUTFString​(java.io.InputStream inputStream)
      Detects optional BOM and reads UTF String from an InputStream.
      static java.lang.String readUTFString​(java.lang.String fileName)  
      static java.lang.String sha256​(java.io.File file)
      Creates SHA-256 digest of a file.
      static java.lang.String sha512​(java.io.File file)
      Creates SHA-512 digest of a file.
      static java.io.File toContainerFile​(java.net.URL url)
      Returns a file of either a folder or a containing archive.
      static java.io.File toFile​(java.net.URL url)
      Converts File URLs to File.
      static java.lang.String toFileName​(java.net.URL url)
      Converts File URLs to file name.
      static void touch​(java.io.File file)
      Implements the Unix "touch" utility.
      static void touch​(java.lang.String file)  
      static java.net.URL toURL​(java.io.File file)
      Converts File to URL in a correct way.
      static void writeBytes​(java.io.File dest, byte[] data)  
      static void writeBytes​(java.io.File dest, byte[] data, int off, int len)
      Write bytes.
      static void writeBytes​(java.lang.String dest, byte[] data)  
      static void writeBytes​(java.lang.String dest, byte[] data, int off, int len)  
      static void writeChars​(java.io.File dest, char[] data)  
      static void writeChars​(java.io.File dest, char[] data, java.nio.charset.Charset encoding)
      Write characters.
      static void writeChars​(java.lang.String dest, char[] data)  
      static void writeChars​(java.lang.String dest, char[] data, java.nio.charset.Charset encoding)  
      static void writeStream​(java.io.FileOutputStream out, java.io.InputStream in)
      Write InputStream in to FileOutputStream.
      static void writeStream​(java.io.File dest, java.io.InputStream in)  
      static void writeStream​(java.lang.String dest, java.io.InputStream in)  
      static void writeString​(java.io.File dest, java.lang.String data)  
      static void writeString​(java.io.File dest, java.lang.String data, java.nio.charset.Charset encoding)
      Writes String.
      static void writeString​(java.lang.String dest, java.lang.String data)  
      static void writeString​(java.lang.String dest, java.lang.String data, java.nio.charset.Charset encoding)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • tempFilePrefix

        public static java.lang.String tempFilePrefix
        Default prefix for temporary files.
    • Constructor Detail

      • FileUtil

        public FileUtil()
    • Method Detail

      • file

        public static java.io.File file​(java.lang.String fileName)
        Simple factory for File objects but with home resolving.
      • equals

        public static boolean equals​(java.lang.String one,
                                     java.lang.String two)
        See Also:
        equals(File, File)
      • equals

        public static boolean equals​(java.io.File one,
                                     java.io.File two)
        Checks if two Files point to the same File.
        Parameters:
        one - File one.
        two - File two.
        Returns:
        true if the Files match.
      • toFile

        public static java.io.File toFile​(java.net.URL url)
        Converts File URLs to File. Ignores other schemes and returns null.
      • toURL

        public static java.net.URL toURL​(java.io.File file)
                                  throws java.net.MalformedURLException
        Converts File to URL in a correct way.
        Returns:
        URL or null in case of error.
        Throws:
        java.net.MalformedURLException - if File cannot be converted.
      • toFileName

        public static java.lang.String toFileName​(java.net.URL url)
        Converts File URLs to file name. Accepts only URLs with 'file' protocol. Otherwise, for other schemes returns null.
        Parameters:
        url - URL to convert
        Returns:
        file name
      • toContainerFile

        public static java.io.File toContainerFile​(java.net.URL url)
        Returns a file of either a folder or a containing archive.
      • isExistingFile

        public static boolean isExistingFile​(java.io.File file)
        Returns true if File exists.
      • isExistingFolder

        public static boolean isExistingFolder​(java.io.File folder)
        Returns true if directory exists.
      • mkdirs

        public static java.io.File mkdirs​(java.lang.String dirs)
                                   throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        mkdirs(File)
      • mkdirs

        public static java.io.File mkdirs​(java.io.File dirs)
                                   throws java.io.IOException
        Creates all directories at once.
        Parameters:
        dirs - Directories to make.
        Throws:
        java.io.IOException - if cannot create directory.
      • mkdir

        public static java.io.File mkdir​(java.lang.String dir)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        mkdir(File)
      • mkdir

        public static java.io.File mkdir​(java.io.File dir)
                                  throws java.io.IOException
        Creates single directory.
        Throws:
        java.io.IOException - if cannot create directory.
      • touch

        public static void touch​(java.lang.String file)
                          throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        touch(File)
      • touch

        public static void touch​(java.io.File file)
                          throws java.io.IOException
        Implements the Unix "touch" utility. It creates a new File with size 0 or, if the File exists already, it is opened and closed without modifying it, but updating the File date and time.
        Throws:
        java.io.IOException
      • copyFile

        public static void copyFile​(java.lang.String srcFile,
                                    java.lang.String destFile)
                             throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copyFile(File, File)
      • copyFile

        public static void copyFile​(java.io.File srcFile,
                                    java.io.File destFile)
                             throws java.io.IOException
        Copies a File to another File.
        Parameters:
        srcFile - Source File.
        destFile - Destination File.
        Throws:
        java.io.IOException - if cannot copy
      • copyFileToDir

        public static java.io.File copyFileToDir​(java.lang.String srcFile,
                                                 java.lang.String destDir)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copyFileToDir(File, File)
      • copyFileToDir

        public static java.io.File copyFileToDir​(java.io.File srcFile,
                                                 java.io.File destDir)
                                          throws java.io.IOException
        Copies a File to directory with specified copy params and returns copied destination.
        Throws:
        java.io.IOException
      • copyDir

        public static void copyDir​(java.lang.String srcDir,
                                   java.lang.String destDir)
                            throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copyDir(File, File)
      • copyDir

        public static void copyDir​(java.io.File srcDir,
                                   java.io.File destDir)
                            throws java.io.IOException
        Copies directory with specified copy params.
        Throws:
        java.io.IOException
        See Also:
        _copyDirectory(File, File)
      • moveFile

        public static java.io.File moveFile​(java.lang.String srcFile,
                                            java.lang.String destFile)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        moveFile(File, File)
      • moveFile

        public static java.io.File moveFile​(java.io.File srcFile,
                                            java.io.File destFile)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        _moveFile(File, File)
      • moveFileToDir

        public static java.io.File moveFileToDir​(java.lang.String srcFile,
                                                 java.lang.String destDir)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        moveFileToDir(File, File)
      • moveFileToDir

        public static java.io.File moveFileToDir​(java.io.File srcFile,
                                                 java.io.File destDir)
                                          throws java.io.IOException
        Moves a file to a directory.
        Parameters:
        srcFile - Source File.
        destDir - Destination directory.
        Throws:
        java.io.IOException - if there is an error during move.
      • moveDir

        public static java.io.File moveDir​(java.lang.String srcDir,
                                           java.lang.String destDir)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        moveDir(File, File)
      • moveDir

        public static java.io.File moveDir​(java.io.File srcDir,
                                           java.io.File destDir)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        _moveDirectory(File, File)
      • deleteFile

        public static void deleteFile​(java.lang.String destFile)
                               throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        deleteFile(File)
      • deleteFile

        public static void deleteFile​(java.io.File destFile)
                               throws java.io.IOException
        Deletes a File.
        Parameters:
        destFile - Destination to delete.
        Throws:
        java.io.IOException - if there was an error deleting.
      • deleteDir

        public static void deleteDir​(java.lang.String destDir)
                              throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        deleteDir(File)
      • deleteDir

        public static void deleteDir​(java.io.File destDir)
                              throws java.io.IOException
        Deletes a directory.
        Parameters:
        destDir - Destination to delete.
        Throws:
        java.io.IOException - if there was an error deleting.
      • cleanDir

        public static void cleanDir​(java.lang.String dest)
                             throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        cleanDir(File)
      • cleanDir

        public static void cleanDir​(java.io.File destDir)
                             throws java.io.IOException
        Cleans a directory without deleting it.
        Parameters:
        destDir - destination to clean.
        Throws:
        java.io.IOException - if something went wrong.
      • readUTFChars

        public static char[] readUTFChars​(java.lang.String fileName)
                                   throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readUTFChars(File)
      • readUTFChars

        public static char[] readUTFChars​(java.io.File file)
                                   throws java.io.IOException
        Reads UTF file content as char array.
        Parameters:
        file - File to read.
        Returns:
        array of characters.
        Throws:
        java.io.IOException - if something went wrong.
      • readChars

        public static char[] readChars​(java.io.File file,
                                       java.nio.charset.Charset encoding)
                                throws java.io.IOException
        Reads file content as char array.
        Parameters:
        file - File to read.
        encoding - Encoding to use.
        Returns:
        array of characters.
        Throws:
        java.io.IOException - if something went wrong.
      • readChars

        public static char[] readChars​(java.lang.String fileName)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readChars(String, Charset)
      • readChars

        public static char[] readChars​(java.io.File file)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readChars(File, Charset)
      • readChars

        public static char[] readChars​(java.lang.String fileName,
                                       java.nio.charset.Charset encoding)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readChars(File, Charset)
      • writeChars

        public static void writeChars​(java.io.File dest,
                                      char[] data)
                               throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        writeChars(File, char[], Charset)
      • writeChars

        public static void writeChars​(java.lang.String dest,
                                      char[] data)
                               throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        writeChars(File, char[])
      • writeChars

        public static void writeChars​(java.lang.String dest,
                                      char[] data,
                                      java.nio.charset.Charset encoding)
                               throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        writeChars(File, char[], Charset)
      • writeChars

        public static void writeChars​(java.io.File dest,
                                      char[] data,
                                      java.nio.charset.Charset encoding)
                               throws java.io.IOException
        Write characters. append = false
        Throws:
        java.io.IOException
        See Also:
        outChars(File, char[], Charset, boolean)
      • outChars

        protected static void outChars​(java.io.File dest,
                                       char[] data,
                                       java.nio.charset.Charset encoding,
                                       boolean append)
                                throws java.io.IOException
        Writes characters to File destination.
        Parameters:
        dest - destination File
        data - Data as a String
        encoding - Encoding as a String
        append - true if appending; false if File should be overwritten.
        Throws:
        java.io.IOException - if something went wrong.
      • readUTFString

        public static java.lang.String readUTFString​(java.lang.String fileName)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readUTFString(File)
      • readUTFString

        public static java.lang.String readUTFString​(java.io.File file)
                                              throws java.io.IOException
        Detects optional BOM and reads UTF String from a File. If BOM is missing, UTF-8 is assumed.
        Parameters:
        file - File to read.
        Returns:
        String in UTF encoding.
        Throws:
        java.io.IOException - if copy to InputStream errors.
        See Also:
        unicodeInputStreamOf(File), IOUtil.copy(InputStream, Charset)
      • readUTFString

        public static java.lang.String readUTFString​(java.io.InputStream inputStream)
                                              throws java.io.IOException
        Detects optional BOM and reads UTF String from an InputStream. If BOM is missing, UTF-8 is assumed.
        Parameters:
        inputStream - InputStream to read.
        Returns:
        String in UTF encoding.
        Throws:
        java.io.IOException - if copy to InputStream errors.
        See Also:
        unicodeInputStreamOf(File), IOUtil.copy(InputStream, Charset)
      • readString

        public static java.lang.String readString​(java.io.File file,
                                                  java.nio.charset.Charset encoding)
                                           throws java.io.IOException
        Reads File content as String encoded in provided encoding. For UTF encoded files, detects optional BOM characters.
        Parameters:
        file - File to read.
        encoding - Encoding to use.
        Returns:
        String representing File content.
        Throws:
        java.io.IOException - if copy to InputStream errors.
        See Also:
        streamOf(File, Charset), IOUtil.copy(InputStream, Charset)
      • readString

        public static java.lang.String readString​(java.lang.String source)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readString(String, Charset)
      • readString

        public static java.lang.String readString​(java.lang.String source,
                                                  java.nio.charset.Charset encoding)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readString(File, Charset)
      • readString

        public static java.lang.String readString​(java.io.File source)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readString(File, Charset)
      • writeString

        public static void writeString​(java.lang.String dest,
                                       java.lang.String data)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        writeString(File, String, Charset)
      • writeString

        public static void writeString​(java.lang.String dest,
                                       java.lang.String data,
                                       java.nio.charset.Charset encoding)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        writeString(File, String, Charset)
      • writeString

        public static void writeString​(java.io.File dest,
                                       java.lang.String data)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        writeString(File, String, Charset)
      • writeString

        public static void writeString​(java.io.File dest,
                                       java.lang.String data,
                                       java.nio.charset.Charset encoding)
                                throws java.io.IOException
        Writes String. append = false
        Throws:
        java.io.IOException
        See Also:
        outString(File, String, Charset, boolean)
      • appendString

        public static void appendString​(java.lang.String dest,
                                        java.lang.String data)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        appendString(File, String)
      • appendString

        public static void appendString​(java.lang.String dest,
                                        java.lang.String data,
                                        java.nio.charset.Charset encoding)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        appendString(File, String, Charset)
      • appendString

        public static void appendString​(java.io.File dest,
                                        java.lang.String data)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        appendString(File, String, Charset)
      • appendString

        public static void appendString​(java.io.File dest,
                                        java.lang.String data,
                                        java.nio.charset.Charset encoding)
                                 throws java.io.IOException
        Appends String. append = true
        Throws:
        java.io.IOException
        See Also:
        outString(File, String, Charset, boolean)
      • outString

        protected static void outString​(java.io.File dest,
                                        java.lang.String data,
                                        java.nio.charset.Charset encoding,
                                        boolean append)
                                 throws java.io.IOException
        Writes data using encoding to File.
        Parameters:
        dest - destination File
        data - Data as a String
        encoding - Encoding as a String
        append - true if appending; false if File should be overwritten.
        Throws:
        java.io.IOException - if something went wrong.
      • writeStream

        public static void writeStream​(java.lang.String dest,
                                       java.io.InputStream in)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        writeStream(File, InputStream)
      • writeStream

        public static void writeStream​(java.io.FileOutputStream out,
                                       java.io.InputStream in)
                                throws java.io.IOException
        Write InputStream in to FileOutputStream.
        Parameters:
        out - FileOutputStream to write to.
        in - InputStream to read.
        Throws:
        java.io.IOException - if there is an issue reading/writing.
      • readLines

        public static java.lang.String[] readLines​(java.lang.String source)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readLines(String, Charset)
      • readLines

        public static java.lang.String[] readLines​(java.lang.String source,
                                                   java.nio.charset.Charset encoding)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readLines(File, Charset)
      • readLines

        public static java.lang.String[] readLines​(java.io.File source)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readLines(File, Charset)
      • readLines

        public static java.lang.String[] readLines​(java.io.File file,
                                                   java.nio.charset.Charset encoding)
                                            throws java.io.IOException
        Reads lines from source File with specified encoding and returns lines as Strings in array.
        Parameters:
        file - File to read.
        encoding - Endoing to use.
        Returns:
        array of Strings which represents lines in the File.
        Throws:
        java.io.IOException - if File does not exist or is not a File or there is an issue reading the File.
      • readBytes

        public static byte[] readBytes​(java.lang.String file)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readBytes(File)
      • readBytes

        public static byte[] readBytes​(java.io.File file)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readBytes(File, int)
      • readBytes

        public static byte[] readBytes​(java.io.File file,
                                       int count)
                                throws java.io.IOException
        Read file and returns byte array with contents.
        Parameters:
        file - File to read
        count - number of bytes to read
        Returns:
        byte array from File contents.
        Throws:
        java.io.IOException - if not a File or File does not exist or file size is larger than Integer.MAX_VALUE.
      • writeBytes

        public static void writeBytes​(java.lang.String dest,
                                      byte[] data)
                               throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        writeBytes(File, byte[])
      • writeBytes

        public static void writeBytes​(java.io.File dest,
                                      byte[] data)
                               throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        writeBytes(File, byte[], int, int)
      • writeBytes

        public static void writeBytes​(java.lang.String dest,
                                      byte[] data,
                                      int off,
                                      int len)
                               throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        writeBytes(File, byte[], int, int)
      • writeBytes

        public static void writeBytes​(java.io.File dest,
                                      byte[] data,
                                      int off,
                                      int len)
                               throws java.io.IOException
        Write bytes. append = false
        Throws:
        java.io.IOException
        See Also:
        outBytes(File, byte[], int, int, boolean)
      • appendBytes

        public static void appendBytes​(java.lang.String dest,
                                       byte[] data)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        appendBytes(File, byte[])
      • appendBytes

        public static void appendBytes​(java.lang.String dest,
                                       byte[] data,
                                       int off,
                                       int len)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        appendBytes(File, byte[], int, int)
      • appendBytes

        public static void appendBytes​(java.io.File dest,
                                       byte[] data)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        appendBytes(File, byte[], int, int)
      • appendBytes

        public static void appendBytes​(java.io.File dest,
                                       byte[] data,
                                       int off,
                                       int len)
                                throws java.io.IOException
        Appends bytes. append = true
        Throws:
        java.io.IOException
        See Also:
        outBytes(File, byte[], int, int, boolean)
      • outBytes

        protected static void outBytes​(java.io.File dest,
                                       byte[] data,
                                       int off,
                                       int len,
                                       boolean append)
                                throws java.io.IOException
        Writes data to File destination.
        Parameters:
        dest - destination File
        data - Data as a String
        off - the start offset in the data.
        len - the number of bytes to write.
        append - true if appending; false if File should be overwritten.
        Throws:
        java.io.IOException - if something went wrong.
      • compare

        public static boolean compare​(java.lang.String file1,
                                      java.lang.String file2)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • compare

        public static boolean compare​(java.io.File one,
                                      java.io.File two)
                               throws java.io.IOException
        Compare the contents of two Files to determine if they are equal or not.

        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.

        Code origin: Avalon

        Throws:
        java.io.IOException
      • isOlder

        public static boolean isOlder​(java.lang.String file,
                                      java.lang.String reference)
        See Also:
        isOlder(File, File)
      • isNewer

        public static boolean isNewer​(java.lang.String file,
                                      java.lang.String reference)
        See Also:
        isNewer(File, File)
      • isNewer

        public static boolean isNewer​(java.io.File file,
                                      java.io.File reference)
        Uses File.lastModified() for reference.
        See Also:
        isNewer(File, long)
      • isOlder

        public static boolean isOlder​(java.io.File file,
                                      java.io.File reference)
        Uses File.lastModified() for reference.
        See Also:
        isOlder(File, long)
      • isNewer

        public static boolean isNewer​(java.io.File file,
                                      long timeMillis)
        Tests if the specified File is newer than the specified time reference.
        Parameters:
        file - the File of which the modification date must be compared.
        timeMillis - the time reference measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)
        Returns:
        true if the File exists and has been modified after the given time reference.
      • isNewer

        public static boolean isNewer​(java.lang.String file,
                                      long timeMillis)
        See Also:
        isNewer(File, long)
      • isOlder

        public static boolean isOlder​(java.io.File file,
                                      long timeMillis)
        Tests if the specified File is older than the specified time reference.
        Parameters:
        file - the File of which the modification date must be compared.
        timeMillis - the time reference measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)
        Returns:
        true if the File exists and has been modified after the given time reference.
      • isOlder

        public static boolean isOlder​(java.lang.String file,
                                      long timeMillis)
        See Also:
        isOlder(File, long)
      • copy

        public static void copy​(java.lang.String src,
                                java.lang.String dest)
                         throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copy(File, File)
      • copy

        public static void copy​(java.io.File src,
                                java.io.File dest)
                         throws java.io.IOException
        Smart copy. If source is a directory, copy it to destination. Otherwise, if destination is directory, copy source file to it. Otherwise, try to copy source file to destination file.
        Parameters:
        src - source File
        dest - destination File
        Throws:
        java.io.IOException - if there is an error copying.
        See Also:
        copyDir(File, File), copyFileToDir(File, File), copyFile(File, File)
      • move

        public static void move​(java.lang.String src,
                                java.lang.String dest)
                         throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        move(File, File)
      • move

        public static void move​(java.io.File src,
                                java.io.File dest)
                         throws java.io.IOException
        Smart move. If source is a directory, move it to destination. Otherwise, if destination is directory, move source File to it. Otherwise, try to move source File to destination File.
        Parameters:
        src - source File
        dest - destination File
        Throws:
        java.io.IOException - if there is an error moving.
        See Also:
        moveDir(File, File), moveFileToDir(File, File), moveFile(File, File)
      • delete

        public static void delete​(java.lang.String dest)
                           throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        delete(File)
      • delete

        public static void delete​(java.io.File dest)
                           throws java.io.IOException
        Smart delete of destination file or directory.
        Throws:
        java.io.IOException - if there is an issue deleting the file/directory.
        See Also:
        deleteFile(File), deleteDir(File)
      • isAncestor

        public static boolean isAncestor​(java.io.File ancestor,
                                         java.io.File file,
                                         boolean strict)
        Check if one File is an ancestor of second one.
        Parameters:
        strict - if c then this method returns true if ancestor and File are equal
        Returns:
        true if ancestor is parent of File; otherwise, false
      • getParentFile

        public static java.io.File getParentFile​(java.io.File file)
        Returns parent for the file. The method correctly processes "." and ".." in File names. The name remains relative if was relative before. Returns null if the File has no parent.
        Parameters:
        file - File
        Returns:
        null if the File has no parent.
      • isFilePathAcceptable

        public static boolean isFilePathAcceptable​(java.io.File file,
                                                   java.io.FileFilter fileFilter)
        Checks if file and its ancestors are acceptable by using FileFilter.accept(File).
        Parameters:
        file - File to check.
        fileFilter - FileFilter to use.
        Returns:
        if file and its ancestors are acceptable
      • createTempDirectory

        public static java.io.File createTempDirectory​(java.lang.String prefix,
                                                       java.lang.String suffix)
                                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        createTempDirectory(String, String, File)
      • createTempDirectory

        public static java.io.File createTempDirectory​(java.lang.String prefix,
                                                       java.lang.String suffix,
                                                       java.io.File tempDir)
                                                throws java.io.IOException
        Creates temporary directory.
        Throws:
        java.io.IOException
        See Also:
        createTempFile(String, String, File)
      • createTempFile

        public static java.io.File createTempFile​(java.lang.String prefix,
                                                  java.lang.String suffix,
                                                  java.io.File tempDir,
                                                  boolean create)
                                           throws java.io.IOException
        Creates temporary File.
        Parameters:
        prefix - The prefix string to be used in generating the file's name; must be at least three characters long
        suffix - The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used
        tempDir - The directory in which the file is to be created, or null if the default temporary-file directory is to be used
        create - If create is set to true File will be physically created on the file system. Otherwise, it will be created and then deleted - trick that will make temp File exist only if they are used.
        Returns:
        File
        Throws:
        java.io.IOException
      • createTempFile

        public static java.io.File createTempFile​(java.lang.String prefix,
                                                  java.lang.String suffix,
                                                  java.io.File tempDir)
                                           throws java.io.IOException
        Creates temporary File. Wraps Java method and repeats creation several times if something fails.
        Parameters:
        prefix - The prefix string to be used in generating the file's name; must be at least three characters long
        suffix - The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used
        tempDir - The directory in which the file is to be created, or null if the default temporary-file directory is to be used
        Throws:
        java.io.IOException
      • isSymlink

        @Deprecated
        public static boolean isSymlink​(java.io.File file)
        Deprecated.
        Files.isSymbolicLink(java.nio.file.Path) provides this functionality natively as of Java 1.7.
        Determines whether the specified file is a symbolic link rather than an actual file.
      • md5

        public static java.lang.String md5​(java.io.File file)
                                    throws java.io.IOException
        Creates MD5 digest of a File.
        Parameters:
        file - File to create digest of.
        Returns:
        MD5 digest of the File.
        Throws:
        java.io.IOException
      • sha256

        public static java.lang.String sha256​(java.io.File file)
                                       throws java.io.IOException
        Creates SHA-256 digest of a file.
        Parameters:
        file - File to create digest of.
        Returns:
        SHA-256 digest of the File.
        Throws:
        java.io.IOException
      • sha512

        public static java.lang.String sha512​(java.io.File file)
                                       throws java.io.IOException
        Creates SHA-512 digest of a file.
        Parameters:
        file - File to create digest of.
        Returns:
        SHA-512 digest of the File.
        Throws:
        java.io.IOException
      • isBinary

        public static boolean isBinary​(java.io.File file)
                                throws java.io.IOException
        Checks the start of the file for ASCII control characters
        Parameters:
        file - File
        Returns:
        true if the the start of the File is ASCII control characters.
        Throws:
        java.io.IOException