Class FileUtil


  • public class FileUtil
    extends java.lang.Object
    collection of helper routines to work with files
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.io.File addSecure​(java.io.File baseFile, java.io.File file)
      securely add a subdirectory
      static java.io.File cleanDots​(java.io.File file)
      remove any internal "../" "./" and "//" from a url
      static java.io.File cleanURL​(java.io.File f)  
      static boolean copyBytes​(byte[] buf, java.io.File toFile)
      copy a buffer to the end of a file, creating it if necessary
      static boolean copyFile​(java.io.File fromFile, java.io.File toFile)
      copy a file, unless fromFile and toFile are equal
      static java.io.File copyFileToDir​(java.io.File fromFile, java.io.File toDir)
      copies a File to directory if toFile exists, it is brutally overwritten unless fromFile equals toFile
      static boolean createFile​(java.io.File file)
      similar to File.createFile but also creates any required directories
      static boolean createNewFile​(java.io.File file)
      similar to File.createFile but also creates any required directories
      static boolean deleteAll​(java.io.File dirToZapp)
      very brutal tree zapper that will delete a file or directory tree recursively
      static void dumpException​(java.io.File except, java.lang.Throwable t)  
      static java.io.File ensureFileInDir​(java.io.File fromFile, java.io.File toDir)
      copies a File to directory if and only if toFile does not exist
      static boolean ensureParent​(java.io.File file)  
      static boolean equals​(java.io.File file1, java.io.File file2)
      checks the equivalence of files - todo os specific behavior (just in case)
      static boolean exists​(java.io.File file)  
      static byte[] fileToByteArray​(java.io.File file)
      read a file into a byte array
      static java.lang.String fileToString​(java.io.File file, java.nio.charset.Charset s)
      read a file into a byte array
      static boolean forceDelete​(java.io.File file)
      forces deletion of a file
      static boolean forceDelete​(java.io.File file, int loops)
      forces deletion of a file
      static java.io.File getAuxDir​(java.io.File hotFile)
      get any auxiliary directory with the same name as a file
      static java.io.BufferedInputStream getBufferedInputStream​(java.io.File file)
      create a buffered input stream for a file
      static java.io.BufferedOutputStream getBufferedOutputStream​(java.io.File file)
      create a buffered output stream for a file
      static java.io.BufferedOutputStream getBufferedOutputStream​(java.io.File file, boolean append)
      create a buffered output stream for a file
      static java.io.File getCreateDirectory​(java.lang.String newDir)
      create a new directory and return null if the directory could not be created
      static java.lang.String getExtension​(java.io.File file)  
      static byte[] getFastMD5​(java.io.File f, int maxSize)
      get an md5 from a file that reads at most 2*maxSize bytes of which maxSize are from the front and maxSize are from the back
      static java.io.File getFileInDirectory​(java.io.File dir, java.io.File localFile)
      returns a File object corresponding to an instance of localFile placed in dir - No OS calls are made and File is NOT created
      static java.lang.String getSecureFileName​(java.io.File file)
      secure check of a file name
      static java.lang.String getSecureName​(java.io.File file)
      secure check of a file name
      static java.lang.String getSecurePath​(java.io.File file, boolean allowAbsolute)
      secure check of a file path
      static boolean isAbsoluteFile​(java.io.File f)
      check whether a file is absolute
      static boolean isAbsoluteFile​(java.lang.String f)  
      static boolean isDirectory​(java.io.File f)
      check whether a file is a directory.
      static boolean isDirectory​(java.lang.String s)
      check whether a file is a directory.
      static boolean isFile​(java.io.File file)  
      static boolean isLocked​(java.io.File file)  
      static boolean isWindows()
      Deprecated.
      static java.io.File[] listDirectories​(java.io.File dir)
      list all direct child directories
      static java.util.Vector<java.io.File> listFilesInTree​(java.io.File dir, java.io.FileFilter filter)
      list all files matching given regexp
      static java.util.Vector<java.io.File> listFilesInTree​(java.io.File dir, java.lang.String expression)
      list all files matching given regexp
      static java.io.File[] listFilesWithExpression​(java.io.File dir, java.lang.String expression)
      list all files matching given regexp
      static java.io.File[] listFilesWithExpression​(java.io.File dir, java.lang.String expression, int max)
      list all files matching given regexp
      static java.io.File[] listFilesWithExtension​(java.io.File dir, java.lang.String allExtensions)  
      static java.io.File[] listFilesWithExtension​(java.io.File dir, java.lang.String extension, int max)
      list all files with a given extension (directories are skipped
      static boolean moveFile​(java.io.File fromFile, java.io.File toFile)
      moves a File by trying to rename, if this fails, a copy with subsequent delete is performed.
      static java.io.File moveFileToDir​(java.io.File fromFile, java.io.File toDir)
      moves a File to directory by trying to rename, if this fails, a copy with subsequent delete is performed.
      static java.io.File newExtension​(java.io.File f, java.lang.String newExt)
      create a File object with a new extension
      static int numFiles​(java.io.File dir, int max)
      number of files - at most max
      static java.io.File streamToFile​(java.io.InputStream fis, java.io.File fil)  
      static java.io.File streamToFile​(java.io.InputStream fis, java.lang.String fileName)
      dump a stream to a newly created file
      static MyPair<java.io.File,​byte[]> streamToMD5File​(java.io.InputStream fis, java.io.File fil)
      same as streanToFile but also calculates the md5 hash of the stream
      static java.io.File stringToFile​(java.lang.String s, java.io.File fil)  
      static java.io.File writeFile​(IStreamWriter w, java.io.File file)
      write to a file
      • Methods inherited from class java.lang.Object

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

      • isLocked

        public static boolean isLocked​(java.io.File file)
      • exists

        public static boolean exists​(java.io.File file)
      • isFile

        public static boolean isFile​(java.io.File file)
      • getAuxDir

        public static java.io.File getAuxDir​(java.io.File hotFile)
        get any auxiliary directory with the same name as a file
        Parameters:
        hotFile -
        Returns:
      • listFilesWithExtension

        public static java.io.File[] listFilesWithExtension​(java.io.File dir,
                                                            java.lang.String extension,
                                                            int max)
        list all files with a given extension (directories are skipped
        Parameters:
        dir - the directory to search
        extension - comma separated list of extensions to check for (null = list all)
        Returns:
        Files[] the matching files, null if none are found
      • listFilesWithExtension

        public static java.io.File[] listFilesWithExtension​(java.io.File dir,
                                                            java.lang.String allExtensions)
      • listFilesWithExpression

        public static java.io.File[] listFilesWithExpression​(java.io.File dir,
                                                             java.lang.String expression)
        list all files matching given regexp
        Parameters:
        dir - the directory to search
        expression - regular expression - uses the simplified syntax
        Returns:
        Files[] the matching files, null if none are found
      • listFilesWithExpression

        public static java.io.File[] listFilesWithExpression​(java.io.File dir,
                                                             java.lang.String expression,
                                                             int max)
        list all files matching given regexp
        Parameters:
        dir - the directory to search
        expression - regular expression - uses the simplified syntax
        Returns:
        Files[] the matching files, null if none are found
      • numFiles

        public static int numFiles​(java.io.File dir,
                                   int max)
        number of files - at most max
        Parameters:
        dir - the directory to search
        expression - regular expression - uses the simplified syntax
        Returns:
        Files[] the matching files, null if none are found
      • listFilesInTree

        public static java.util.Vector<java.io.File> listFilesInTree​(java.io.File dir,
                                                                     java.io.FileFilter filter)
        list all files matching given regexp
        Parameters:
        dir - the directory to search
        filter - the filter to apply to files
        Returns:
        Vector the matching files, null if none are found
      • listFilesInTree

        public static java.util.Vector<java.io.File> listFilesInTree​(java.io.File dir,
                                                                     java.lang.String expression)
        list all files matching given regexp
        Parameters:
        dir - the directory to search
        expression - comma separated list of regular expression of a tree with slashes separating directories
        Returns:
        Files[] the matching files, null if none are found
      • listDirectories

        public static java.io.File[] listDirectories​(java.io.File dir)
        list all direct child directories
        Parameters:
        dir - the directory to search
        Returns:
        Files[] the matching directories, null if none are found
      • deleteAll

        public static boolean deleteAll​(java.io.File dirToZapp)
        very brutal tree zapper that will delete a file or directory tree recursively
        Parameters:
        dirToZapp - the file directory to utterly anihilate
        Returns:
        true if ciao
      • streamToFile

        public static java.io.File streamToFile​(java.io.InputStream fis,
                                                java.lang.String fileName)
        dump a stream to a newly created file
        Parameters:
        fis - the inputstream to read
        fileName - the file to stream to
        Returns:
        the file created by the stream, null if snafu
      • writeFile

        public static java.io.File writeFile​(IStreamWriter w,
                                             java.io.File file)
        write to a file
        Parameters:
        file - the file to write
        w - the writer to write to
        Returns:
        the file that was created, null if snafu
      • newExtension

        public static java.io.File newExtension​(java.io.File f,
                                                java.lang.String newExt)
        create a File object with a new extension
        Parameters:
        f - the file, if null always returns null
        newExt - the new extension
        Returns:
        the file with the new extension
        See Also:
        for details of handling null etc.
      • streamToFile

        public static java.io.File streamToFile​(java.io.InputStream fis,
                                                java.io.File fil)
        Parameters:
        fis - the InputStream to read - if null nothing happens
        fil - the file to stream to
        Returns:
        the file created by the stream, null if snafu
      • stringToFile

        public static java.io.File stringToFile​(java.lang.String s,
                                                java.io.File fil)
        Parameters:
        s - the String to read - if null nothing happens
        fil - the file to stream to
        Returns:
        the file created by the stream, null if snafu
      • getFastMD5

        public static byte[] getFastMD5​(java.io.File f,
                                        int maxSize)
        get an md5 from a file that reads at most 2*maxSize bytes of which maxSize are from the front and maxSize are from the back
        Parameters:
        f -
        maxSize - if<=0 always use entir length
        Returns:
      • streamToMD5File

        public static MyPair<java.io.File,​byte[]> streamToMD5File​(java.io.InputStream fis,
                                                                        java.io.File fil)
        same as streanToFile but also calculates the md5 hash of the stream
        Parameters:
        fis - the InputStream to read - if null nothing happens
        fil - the file to stream to
        Returns:
        the file created by the stream, null if snafu
      • fileToString

        public static java.lang.String fileToString​(java.io.File file,
                                                    java.nio.charset.Charset s)
        read a file into a byte array
        Parameters:
        file - the file to read into a byte array
        Returns:
        the correctly sized byte array, null if no bytes were read
      • fileToByteArray

        public static byte[] fileToByteArray​(java.io.File file)
        read a file into a byte array
        Parameters:
        file - the file to read into a byte array
        Returns:
        the correctly sized byte array, null if no bytes were read
      • moveFileToDir

        public static java.io.File moveFileToDir​(java.io.File fromFile,
                                                 java.io.File toDir)
        moves a File to directory by trying to rename, if this fails, a copy with subsequent delete is performed. if toFile exists, it is brutally overwritten
        Parameters:
        fromFile - the File to move
        toDir - the Directory to move to
        Returns:
        File the moved File if success, else null, i.e. toFile exists with the contents of fromFile
      • moveFile

        public static boolean moveFile​(java.io.File fromFile,
                                       java.io.File toFile)
        moves a File by trying to rename, if this fails, a copy with subsequent delete is performed. if toFile exists, it is brutally overwritten
        Parameters:
        fromFile - the File to move
        toFile - the File to create
        Returns:
        boolean true if success, i.e. toFile exists with the contents of fromFile
      • copyBytes

        public static boolean copyBytes​(byte[] buf,
                                        java.io.File toFile)
        copy a buffer to the end of a file, creating it if necessary
        Parameters:
        buf - the source buffer
        toFile - the destination File
        Returns:
        true if success
      • copyFile

        public static boolean copyFile​(java.io.File fromFile,
                                       java.io.File toFile)
        copy a file, unless fromFile and toFile are equal
        Parameters:
        fromFile - the source File
        toFile - the destination File
        Returns:
        true if success
      • ensureFileInDir

        public static java.io.File ensureFileInDir​(java.io.File fromFile,
                                                   java.io.File toDir)
        copies a File to directory if and only if toFile does not exist
        Parameters:
        fromFile - the File to move
        toDir - the Directory to move to
        Returns:
        File the destination File if success,
      • cleanDots

        public static java.io.File cleanDots​(java.io.File file)
        remove any internal "../" "./" and "//" from a url
        Parameters:
        file - the file to clean
        Returns:
        File - the clean file
      • copyFileToDir

        public static java.io.File copyFileToDir​(java.io.File fromFile,
                                                 java.io.File toDir)
        copies a File to directory if toFile exists, it is brutally overwritten unless fromFile equals toFile
        Parameters:
        fromFile - the File to move
        toDir - the Directory to move to
        Returns:
        File the moved File if success, else null, i.e. toFile exists with the contents of fromFile
      • getFileInDirectory

        public static java.io.File getFileInDirectory​(java.io.File dir,
                                                      java.io.File localFile)
        returns a File object corresponding to an instance of localFile placed in dir - No OS calls are made and File is NOT created
        Parameters:
        dir - the File Object representing the directory
        localFile - the local file to place in dir, note that only the path is copied - this does copy trees
        Returns:
        File the File object that represents localFile in Dir
      • addSecure

        public static java.io.File addSecure​(java.io.File baseFile,
                                             java.io.File file)
                                      throws java.lang.IllegalArgumentException
        securely add a subdirectory
        Parameters:
        baseFile -
        file -
        Returns:
        Throws:
        java.lang.IllegalArgumentException
      • getSecureName

        public static java.lang.String getSecureName​(java.io.File file)
                                              throws java.lang.IllegalArgumentException
        secure check of a file name
        Parameters:
        file -
        Returns:
        Throws:
        java.lang.IllegalArgumentException
      • getSecureFileName

        public static java.lang.String getSecureFileName​(java.io.File file)
        secure check of a file name
        Parameters:
        file -
        Returns:
        Throws:
        java.lang.IllegalArgumentException
      • getSecurePath

        public static java.lang.String getSecurePath​(java.io.File file,
                                                     boolean allowAbsolute)
                                              throws java.lang.IllegalArgumentException
        secure check of a file path
        Parameters:
        baseFile -
        file -
        Returns:
        Throws:
        java.lang.IllegalArgumentException
      • forceDelete

        public static boolean forceDelete​(java.io.File file)
        forces deletion of a file
        Parameters:
        file - the file to delete
        Returns:
        true if the file no longer exists
      • forceDelete

        public static boolean forceDelete​(java.io.File file,
                                          int loops)
        forces deletion of a file
        Parameters:
        file - the file to delete
        Returns:
        true if the file no longer exists
      • getCreateDirectory

        public static java.io.File getCreateDirectory​(java.lang.String newDir)
        create a new directory and return null if the directory could not be created
        Parameters:
        newDir - the path or URL of the new directory
        Returns:
      • cleanURL

        public static java.io.File cleanURL​(java.io.File f)
        Parameters:
        f - the file to cleanup
        Returns:
        the cleaned file
      • isDirectory

        public static boolean isDirectory​(java.io.File f)
        check whether a file is a directory. If a unix file is a symbolic link to a directory, it is also assumed to be a directory
        Parameters:
        f - the file to test
        Returns:
        true if f is a directory or link to a directory
      • isDirectory

        public static boolean isDirectory​(java.lang.String s)
        check whether a file is a directory. If a unix file is a symbolic link to a directory, it is also assumed to be a directory
        Parameters:
        s - the file to test
        Returns:
        true if f is a directory or link to a directory
      • isAbsoluteFile

        public static boolean isAbsoluteFile​(java.io.File f)
        check whether a file is absolute
        Parameters:
        f - the file to test
        Returns:
        true if f is absolute;
      • isAbsoluteFile

        public static boolean isAbsoluteFile​(java.lang.String f)
        Parameters:
        f - the file path to test
        Returns:
        true if s is absolute
      • getExtension

        public static java.lang.String getExtension​(java.io.File file)
        Parameters:
        file -
        Returns:
        the file extension
      • createNewFile

        public static boolean createNewFile​(java.io.File file)
        similar to File.createFile but also creates any required directories
        Parameters:
        file - the file to create
        Returns:
        true if the file exists after the call, else false
      • createFile

        public static boolean createFile​(java.io.File file)
                                  throws java.io.IOException
        similar to File.createFile but also creates any required directories
        Parameters:
        file - the file to create
        Returns:
        true if the file exists after the call, else false
        Throws:
        java.io.IOException
      • ensureParent

        public static boolean ensureParent​(java.io.File file)
      • equals

        public static boolean equals​(java.io.File file1,
                                     java.io.File file2)
        checks the equivalence of files - todo os specific behavior (just in case)
        Parameters:
        file1 -
        file2 -
        Returns:
      • getBufferedInputStream

        public static java.io.BufferedInputStream getBufferedInputStream​(java.io.File file)
        create a buffered input stream for a file
        Parameters:
        file -
        Returns:
        the buffered input stream, null if snafu
      • getBufferedOutputStream

        public static java.io.BufferedOutputStream getBufferedOutputStream​(java.io.File file)
        create a buffered output stream for a file
        Parameters:
        file -
        Returns:
        the buffered output stream, null if snafu
      • getBufferedOutputStream

        public static java.io.BufferedOutputStream getBufferedOutputStream​(java.io.File file,
                                                                           boolean append)
        create a buffered output stream for a file
        Parameters:
        file -
        append -
        Returns:
        the buffered output stream, null if snafu
      • isWindows

        @Deprecated
        public static boolean isWindows()
        Deprecated.
        Returns:
        true if we are on a windows file system
      • dumpException

        public static void dumpException​(java.io.File except,
                                         java.lang.Throwable t)