Class FileUtils


  • public class FileUtils
    extends java.lang.Object
    This class provides basic facilities for manipulating files and file paths.

    Path-related methods

    Methods exist to retrieve the components of a typical file path. For example /www/hosted/mysite/index.html, can be broken into:

    File-related methods

    There are methods to create a File from a URL, copy a copy a File to another File, copy a URL's contents to a File, as well as methods to delete and clean a directory.

    Common File manipulation routines.

    Taken from the commons-utils repo. Also code from Alexandria's FileUtils. And from Avalon Excalibur's IO. And from Ant.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  FileUtils.FilterWrapper
      Wrapper class for Filter.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int FILE_COPY_BUFFER_SIZE
      The file copy buffer size (30 MB)
      private static java.lang.String FS
      The vm line separator
      private static java.lang.String[] INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
      Non-valid Characters for naming files, folders under Windows: ":", "*", "?", "\"", "<", ">", "|"
      private static int ONE_KB
      The number of bytes in a kilobyte.
      private static int ONE_MB
      The number of bytes in a megabyte.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected FileUtils()
      protected constructor.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      private static java.util.List<java.lang.String> blendFilesToList​(java.util.List<java.lang.String> v, java.lang.String... files)
      Private helper method for getFilesFromExtension()
      private static java.nio.charset.Charset charset​(java.lang.String encoding)
      Returns the named charset or the default charset.
      static void cleanDirectory​(java.io.File directory)
      Deprecated.
      use org.apache.commons.io.FileUtils.cleanDirectory()
      static boolean contentEquals​(java.io.File file1, java.io.File file2)
      Compare the contents of two files to determine if they are equal or not.
      static void copyDirectory​(java.io.File sourceDirectory, java.io.File destinationDirectory)
      Copy the contents of a directory into another one.
      static void copyDirectory​(java.io.File sourceDirectory, java.io.File destinationDirectory, java.lang.String includes, java.lang.String excludes)
      Copy the contents of a directory into another one.
      static void copyDirectoryStructure​(java.io.File sourceDirectory, java.io.File destinationDirectory)
      Deprecated.
      use org.apache.commons.io.FileUtils.copyDirectory()
      private static void copyDirectoryStructure​(java.io.File sourceDirectory, java.io.File destinationDirectory, java.io.File rootDestinationDirectory, boolean onlyModifiedFiles)  
      static void copyFile​(java.io.File source, java.io.File destination)
      Deprecated.
      use java.nio.Files.copy(source.toPath(), destination.toPath(), CopyOptions.NOFOLLOW_LINKS, CopyOptions.REPLACE_EXISTING)
      static void copyFile​(java.io.File from, java.io.File to, java.lang.String encoding, FileUtils.FilterWrapper... wrappers)
      If wrappers is null or empty, the file will be copied only if to.lastModified() < from.lastModified()
      static void copyFile​(java.io.File from, java.io.File to, java.lang.String encoding, FileUtils.FilterWrapper[] wrappers, boolean overwrite)
      If wrappers is null or empty, the file will be copy only if to.lastModified() < from.lastModified() or if overwrite is true
      private static boolean copyFileIfModified​(java.io.File source, java.io.File destination)
      Copy file from source to destination only if source timestamp is later than the destination timestamp.
      private static void copyFilePermissions​(java.io.File source, java.io.File destination)
      Attempts to copy file permissions from the source to the destination file.
      static void copyFileToDirectory​(java.io.File source, java.io.File destinationDirectory)
      Deprecated.
      use org.apache.commons.io.FileUtils.copyFileToDirectory()
      private static void copyFileToDirectoryIfModified​(java.io.File source, java.io.File destinationDirectory)
      Copy file from source to destination only if source is newer than the target file.
      private static void copyStreamToFile​(java.io.InputStream source, java.io.File destination)
      Deprecated.
      use java.nio.Files.copy(source, destination.toPath(), CopyOptions.REPLACE_EXISTING)
      static void copyURLToFile​(java.net.URL source, java.io.File destination)
      Deprecated.
      use java.nio.Files.copy(source.openStream(), destination.toPath(), CopyOptions.REPLACE_EXISTING)
      static java.io.File createSymbolicLink​(java.io.File symlink, java.io.File target)
      Create a new symbolic link, possibly replacing an existing symbolic link.
      static java.io.File createTempFile​(java.lang.String prefix, java.lang.String suffix, java.io.File parentDir)
      Deprecated.
      use java.nio.Files.createTempFile()
      static void delete​(java.io.File file)
      Deprecated.
      use java.nio.files.Files.delete(file.toPath())
      static void deleteDirectory​(java.io.File directory)
      Deprecated.
      use org.apache.commons.io.FileUtils.deleteDirectory()
      static void deleteDirectory​(java.lang.String directory)
      Deprecated.
      use org.apache.commons.io.FileUtils.deleteDirectory()
      private static boolean deleteFile​(java.io.File file)
      Accommodate Windows bug encountered in both Sun and IBM JDKs.
      static boolean deleteLegacyStyle​(java.io.File file)
      Deprecated.
      use java.nio.files.Files.delete(file.toPath())
      static java.lang.String dirname​(java.lang.String path)
      Deprecated.
      use Paths.get(path).getParent().getName()
      private static void doCopyFile​(java.io.File source, java.io.File destination)  
      static java.lang.String extension​(java.lang.String path)
      Deprecated.
      use org.apache.commons.io.FilenameUtils.getExtension
      static void fileAppend​(java.lang.String fileName, java.lang.String data)
      Deprecated.
      use java.nio.files.Files.write(filename, data.getBytes(), StandardOpenOption.APPEND, StandardOpenOption.CREATE)
      static void fileAppend​(java.lang.String fileName, java.lang.String encoding, java.lang.String data)
      Deprecated.
      use java.nio.files.Files.write(filename, data.getBytes(encoding), StandardOpenOption.APPEND, StandardOpenOption.CREATE)
      static void fileDelete​(java.lang.String fileName)
      Deprecated.
      use Files.delete(Paths.get(fileName))
      static boolean fileExists​(java.lang.String fileName)
      Deprecated.
      use java.io.File.exists()
      static java.lang.String filename​(java.lang.String path)
      Deprecated.
      use Paths.get(path).getName()
      static java.lang.String fileRead​(java.io.File file)
      Deprecated.
      use new String(java.nio.files.Files.readAllBytes(file.toPath()))
      static java.lang.String fileRead​(java.io.File file, java.lang.String encoding)
      Deprecated.
      use new String(java.nio.files.Files.readAllBytes(file.toPath()), encoding)
      static java.lang.String fileRead​(java.lang.String file)
      Deprecated.
      use new String(java.nio.files.Files.readAllBytes(file))
      private static java.lang.String fileRead​(java.lang.String file, java.lang.String encoding)
      Deprecated.
      use new String(java.nio.files.Files.readAllBytes(Paths.get(file)), encoding)
      static java.lang.String[] fileReadArray​(java.io.File file)
      Deprecated.
      use java.nio.files.Files.readAllLines()
      static void fileWrite​(java.io.File file, java.lang.String encoding, java.lang.String data)
      Deprecated.
      use java.nio.files.Files.write(file.toPath(), data.getBytes(encoding), StandardOpenOption.CREATE)
      static void fileWrite​(java.lang.String fileName, java.lang.String data)
      Deprecated.
      use java.nio.files.Files.write(filename, data.getBytes(), StandardOpenOption.CREATE)
      static void fileWrite​(java.lang.String fileName, java.lang.String encoding, java.lang.String data)
      Deprecated.
      use java.nio.files.Files.write(Paths.get(filename), data.getBytes(encoding), StandardOpenOption.CREATE)
      static void fileWriteArray​(java.io.File file, java.lang.String... data)
      Deprecated.
      use java.nio.files.Files.write(file.toPath(), data.getBytes(encoding), StandardOpenOption.CREATE)
      static void fileWriteArray​(java.io.File file, java.lang.String encoding, java.lang.String... data)
      Deprecated.
      use java.nio.files.Files.write(file.toPath(), data.getBytes(encoding), StandardOpenOption.CREATE)
      static void forceDelete​(java.io.File file)
      Deprecated.
      use org.apache.commons.io.FileUtils.deleteQuietly()
      static void forceDelete​(java.lang.String file)
      Deprecated.
      use org.apache.commons.io.FileUtils.deleteQuietly()
      static void forceMkdir​(java.io.File file)
      Make a directory.
      static java.lang.String[] getDefaultExcludes()  
      static java.util.List<java.lang.String> getDefaultExcludesAsList()  
      static java.lang.String getDefaultExcludesAsString()  
      static java.util.List<java.lang.String> getDirectoryNames​(java.io.File directory, java.lang.String includes, java.lang.String excludes, boolean includeBasedir)
      Return a list of directories as String depending options.
      static java.util.List<java.lang.String> getDirectoryNames​(java.io.File directory, java.lang.String includes, java.lang.String excludes, boolean includeBasedir, boolean isCaseSensitive)
      Return a list of directories as Strings.
      static java.lang.String getExtension​(java.lang.String filename)
      Deprecated.
      use org.apache.commons.io.FilenameUtils.getExtension()
      static java.util.List<java.lang.String> getFileAndDirectoryNames​(java.io.File directory, java.lang.String includes, java.lang.String excludes, boolean includeBasedir, boolean isCaseSensitive, boolean getFiles, boolean getDirectories)
      Return a list of file names as Strings.
      static java.util.List<java.lang.String> getFileNames​(java.io.File directory, java.lang.String includes, java.lang.String excludes, boolean includeBasedir)
      Return a list of files as String depending options.
      private static java.util.List<java.lang.String> getFileNames​(java.io.File directory, java.lang.String includes, java.lang.String excludes, boolean includeBasedir, boolean isCaseSensitive)
      Return a list of files as String depending options.
      static java.util.List<java.io.File> getFiles​(java.io.File directory, java.lang.String includes, java.lang.String excludes)
      Return the files contained in the directory, using inclusion and exclusion Ant patterns, including the directory name in each of the files
      static java.util.List<java.io.File> getFiles​(java.io.File directory, java.lang.String includes, java.lang.String excludes, boolean includeBasedir)
      Return the files contained in the directory, using inclusion and exclusion Ant patterns
      static java.lang.String[] getFilesFromExtension​(java.lang.String directory, java.lang.String... extensions)
      Given a directory and an array of extensions return an array of compliant files.
      static boolean isSymbolicLink​(java.io.File file)
      Deprecated.
      use java.nio.file.Files.isSymbolicLink(file.toPath())
      static boolean isSymbolicLinkForSure​(java.io.File file)
      Deprecated.
      use java.nio.file.Files.isSymbolicLink(file.toPath())
      private static boolean isValidFile​(java.lang.String file, java.lang.String... extensions)
      Checks to see if a file is of a particular type(s).
      private static boolean isValidWindowsFileName​(java.io.File f)
      For Windows OS, check if the file name contains any of the following characters: ":", "*", "?", "\"", "<", ">", "|"
      static java.util.List<java.lang.String> loadFile​(java.io.File file)
      Deprecated.
      assumes the platform default character set
      static void mkdir​(java.lang.String dir)
      Deprecated.
      use java.nio.file.Files.createDirectories(Paths.get(dir))
      private static void mkdirsFor​(java.io.File destination)  
      static java.lang.String normalize​(java.lang.String path)
      Deprecated.
      use org.apache.commons.io.FileNameUtils.normalize()
      private static int positiveRandom​(java.util.Random rand)  
      static java.lang.String removeExtension​(java.lang.String filename)
      Deprecated.
      use org.apache.commons.io.FilenameUtils.removeExtension()
      static void rename​(java.io.File from, java.io.File to)
      Deprecated.
      use java.nio.Files.move()
      static java.io.File resolveFile​(java.io.File baseFile, java.lang.String filename)
      Resolve a file filename to it's canonical form.
      static long sizeOfDirectory​(java.io.File directory)
      Deprecated.
      use org.apache.commons.io.FileUtils.sizeOf()
      static long sizeOfDirectory​(java.lang.String directory)
      Deprecated.
      use org.apache.commons.io.FileUtils.sizeOf()
      static java.io.File toFile​(java.net.URL url)
      Convert from a URL to a File.
      static java.net.URL[] toURLs​(java.io.File... files)
      Convert the array of Files into a list of URLs.
      • Methods inherited from class java.lang.Object

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

      • ONE_KB

        private static final int ONE_KB
        The number of bytes in a kilobyte.
        See Also:
        Constant Field Values
      • ONE_MB

        private static final int ONE_MB
        The number of bytes in a megabyte.
        See Also:
        Constant Field Values
      • FILE_COPY_BUFFER_SIZE

        private static final int FILE_COPY_BUFFER_SIZE
        The file copy buffer size (30 MB)
        See Also:
        Constant Field Values
      • FS

        private static final java.lang.String FS
        The vm line separator
    • Constructor Detail

      • FileUtils

        protected FileUtils()
        protected constructor.
    • Method Detail

      • getDefaultExcludes

        @Nonnull
        public static java.lang.String[] getDefaultExcludes()
        Returns:
        the default excludes pattern
        See Also:
        DirectoryScanner.DEFAULTEXCLUDES
      • getDefaultExcludesAsList

        @Nonnull
        public static java.util.List<java.lang.String> getDefaultExcludesAsList()
        Returns:
        the default excludes pattern as list
        See Also:
        getDefaultExcludes()
      • dirname

        @Deprecated
        @Nonnull
        public static java.lang.String dirname​(@Nonnull
                                               java.lang.String path)
        Deprecated.
        use Paths.get(path).getParent().getName()
        Returns the directory path portion of a file specification string. Matches the equally named unix command.
        Parameters:
        path - the file path
        Returns:
        the directory portion excluding the ending file separator
      • filename

        @Deprecated
        @Nonnull
        public static java.lang.String filename​(@Nonnull
                                                java.lang.String path)
        Deprecated.
        use Paths.get(path).getName()
        Returns the filename portion of a path.
        Parameters:
        path - the file path
        Returns:
        the filename string with extension
      • extension

        @Deprecated
        @Nonnull
        public static java.lang.String extension​(@Nonnull
                                                 java.lang.String path)
        Deprecated.
        use org.apache.commons.io.FilenameUtils.getExtension
        Returns the extension portion of a file path. This is everything after the last dot '.' in the path (NOT including the dot).
        Parameters:
        path - the file path
        Returns:
        the extension of the file
      • fileExists

        @Deprecated
        public static boolean fileExists​(@Nonnull
                                         java.lang.String fileName)
        Deprecated.
        use java.io.File.exists()
        Check if a file exists.
        Parameters:
        fileName - the file path
        Returns:
        true if file exists
      • fileRead

        @Deprecated
        @Nonnull
        public static java.lang.String fileRead​(@Nonnull
                                                java.lang.String file)
                                         throws java.io.IOException
        Deprecated.
        use new String(java.nio.files.Files.readAllBytes(file))
        Note: the file content is read with platform encoding.
        Parameters:
        file - the file path
        Returns:
        the file content using the platform encoding
        Throws:
        java.io.IOException - if any
      • fileRead

        @Deprecated
        @Nonnull
        private static java.lang.String fileRead​(@Nonnull
                                                 java.lang.String file,
                                                 @Nullable
                                                 java.lang.String encoding)
                                          throws java.io.IOException
        Deprecated.
        use new String(java.nio.files.Files.readAllBytes(Paths.get(file)), encoding)
        Parameters:
        file - the file path
        encoding - the wanted encoding
        Returns:
        the file content using the specified encoding
        Throws:
        java.io.IOException - if any
      • fileRead

        @Deprecated
        @Nonnull
        public static java.lang.String fileRead​(@Nonnull
                                                java.io.File file)
                                         throws java.io.IOException
        Deprecated.
        use new String(java.nio.files.Files.readAllBytes(file.toPath()))
        Note: the file content is read with platform encoding.
        Parameters:
        file - the file path
        Returns:
        the file content using the platform encoding
        Throws:
        java.io.IOException - if any
      • fileRead

        @Deprecated
        @Nonnull
        public static java.lang.String fileRead​(@Nonnull
                                                java.io.File file,
                                                @Nullable
                                                java.lang.String encoding)
                                         throws java.io.IOException
        Deprecated.
        use new String(java.nio.files.Files.readAllBytes(file.toPath()), encoding)
        Parameters:
        file - the file path
        encoding - the wanted encoding
        Returns:
        the file content using the specified encoding
        Throws:
        java.io.IOException - if any
      • fileReadArray

        @Deprecated
        @Nonnull
        public static java.lang.String[] fileReadArray​(@Nonnull
                                                       java.io.File file)
                                                throws java.io.IOException
        Deprecated.
        use java.nio.files.Files.readAllLines()
        Parameters:
        file - the file path
        Returns:
        the file content lines as String[] using the system default encoding. An empty List if the file doesn't exist.
        Throws:
        java.io.IOException - in case of failure
      • fileAppend

        @Deprecated
        public static void fileAppend​(@Nonnull
                                      java.lang.String fileName,
                                      @Nonnull
                                      java.lang.String data)
                               throws java.io.IOException
        Deprecated.
        use java.nio.files.Files.write(filename, data.getBytes(), StandardOpenOption.APPEND, StandardOpenOption.CREATE)
        Appends data to a file. The file is created if it does not exist. Note: the data is written with platform encoding.
        Parameters:
        fileName - the path of the file to write
        data - the content to write to the file
        Throws:
        java.io.IOException - if any
      • fileAppend

        @Deprecated
        public static void fileAppend​(@Nonnull
                                      java.lang.String fileName,
                                      @Nullable
                                      java.lang.String encoding,
                                      @Nonnull
                                      java.lang.String data)
                               throws java.io.IOException
        Deprecated.
        use java.nio.files.Files.write(filename, data.getBytes(encoding), StandardOpenOption.APPEND, StandardOpenOption.CREATE)
        Appends data to a file. The file will be created if it does not exist.
        Parameters:
        fileName - the path of the file to write
        encoding - the encoding of the file
        data - the content to write to the file
        Throws:
        java.io.IOException - if any
      • fileWrite

        @Deprecated
        public static void fileWrite​(@Nonnull
                                     java.lang.String fileName,
                                     @Nonnull
                                     java.lang.String data)
                              throws java.io.IOException
        Deprecated.
        use java.nio.files.Files.write(filename, data.getBytes(), StandardOpenOption.CREATE)
        Writes data to a file. The file will be created if it does not exist. Note: the data is written with platform encoding
        Parameters:
        fileName - the path of the file to write
        data - the content to write to the file
        Throws:
        java.io.IOException - if any
      • fileWrite

        @Deprecated
        public static void fileWrite​(@Nonnull
                                     java.lang.String fileName,
                                     @Nullable
                                     java.lang.String encoding,
                                     @Nonnull
                                     java.lang.String data)
                              throws java.io.IOException
        Deprecated.
        use java.nio.files.Files.write(Paths.get(filename), data.getBytes(encoding), StandardOpenOption.CREATE)
        Writes data to a file. The file will be created if it does not exist.
        Parameters:
        fileName - the path of the file to write
        encoding - the encoding of the file
        data - the content to write to the file
        Throws:
        java.io.IOException - if any
      • fileWrite

        @Deprecated
        public static void fileWrite​(@Nonnull
                                     java.io.File file,
                                     @Nullable
                                     java.lang.String encoding,
                                     @Nonnull
                                     java.lang.String data)
                              throws java.io.IOException
        Deprecated.
        use java.nio.files.Files.write(file.toPath(), data.getBytes(encoding), StandardOpenOption.CREATE)
        Writes data to a file. The file will be created if it does not exist.
        Parameters:
        file - the path of the file to write
        encoding - the encoding of the file
        data - the content to write to the file
        Throws:
        java.io.IOException - if any
      • fileWriteArray

        @Deprecated
        public static void fileWriteArray​(@Nonnull
                                          java.io.File file,
                                          @Nullable
                                          java.lang.String... data)
                                   throws java.io.IOException
        Deprecated.
        use java.nio.files.Files.write(file.toPath(), data.getBytes(encoding), StandardOpenOption.CREATE)
        Writes String array data to a file in the systems default encoding. The file will be created if it does not exist.
        Parameters:
        file - the path of the file to write
        data - the content to write to the file
        Throws:
        java.io.IOException - if any
      • fileWriteArray

        @Deprecated
        public static void fileWriteArray​(@Nonnull
                                          java.io.File file,
                                          @Nullable
                                          java.lang.String encoding,
                                          @Nullable
                                          java.lang.String... data)
                                   throws java.io.IOException
        Deprecated.
        use java.nio.files.Files.write(file.toPath(), data.getBytes(encoding), StandardOpenOption.CREATE)
        Writes String array data to a file. The file is created if it does not exist.
        Parameters:
        file - the path of the file to write
        encoding - the encoding of the file
        data - the content to write to the file
        Throws:
        java.io.IOException - if any
      • fileDelete

        @Deprecated
        public static void fileDelete​(@Nonnull
                                      java.lang.String fileName)
        Deprecated.
        use Files.delete(Paths.get(fileName))
        Deletes a file.
        Parameters:
        fileName - the path of the file to delete
      • getFilesFromExtension

        public static java.lang.String[] getFilesFromExtension​(@Nonnull
                                                               java.lang.String directory,
                                                               @Nonnull
                                                               java.lang.String... extensions)
        Given a directory and an array of extensions return an array of compliant files.

        The given extensions should be like "java" and not like ".java".

        Parameters:
        directory - the path of the directory
        extensions - an array of expected extensions
        Returns:
        an array of files for the wanted extensions
      • blendFilesToList

        @Nonnull
        private static java.util.List<java.lang.String> blendFilesToList​(@Nonnull
                                                                         java.util.List<java.lang.String> v,
                                                                         @Nonnull
                                                                         java.lang.String... files)
        Private helper method for getFilesFromExtension()
      • isValidFile

        private static boolean isValidFile​(@Nonnull
                                           java.lang.String file,
                                           @Nonnull
                                           java.lang.String... extensions)
        Checks to see if a file is of a particular type(s). Note that if the file does not have an extension, an empty string ("") is matched for.
      • mkdir

        @Deprecated
        public static void mkdir​(@Nonnull
                                 java.lang.String dir)
        Deprecated.
        use java.nio.file.Files.createDirectories(Paths.get(dir))
        Simple way to make a directory.
        Parameters:
        dir - the directory to create
        Throws:
        java.lang.IllegalArgumentException - if the dir contains illegal Windows characters under Windows OS
        See Also:
        INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
      • contentEquals

        public static boolean contentEquals​(@Nonnull
                                            java.io.File file1,
                                            @Nonnull
                                            java.io.File file2)
                                     throws java.io.IOException
        Compare the contents of two files to determine if they are equal or not.
        Parameters:
        file1 - the first file
        file2 - the second file
        Returns:
        true if the content of the files are equal or they both don't exist, false otherwise
        Throws:
        java.io.IOException - if any
      • toFile

        @Nullable
        public static java.io.File toFile​(@Nullable
                                          java.net.URL url)
        Convert from a URL to a File.
        Parameters:
        url - file URL
        Returns:
        the equivalent File object, or null if the URL's protocol is not file
      • toURLs

        @Nonnull
        public static java.net.URL[] toURLs​(@Nonnull
                                            java.io.File... files)
                                     throws java.io.IOException
        Convert the array of Files into a list of URLs.
        Parameters:
        files - the array of files
        Returns:
        the array of URLs
        Throws:
        java.io.IOException - if an error occurs
      • removeExtension

        @Deprecated
        @Nonnull
        public static java.lang.String removeExtension​(@Nonnull
                                                       java.lang.String filename)
        Deprecated.
        use org.apache.commons.io.FilenameUtils.removeExtension()
        Remove extension from a path. E.g.
         foo.txt    --> foo
         a\b\c.jpg --> a\b\c
         a\b\c     --> a\b\c
         
        Parameters:
        filename - the path of the file
        Returns:
        the filename minus extension
      • getExtension

        @Deprecated
        @Nonnull
        public static java.lang.String getExtension​(@Nonnull
                                                    java.lang.String filename)
        Deprecated.
        use org.apache.commons.io.FilenameUtils.getExtension()
        Get extension from a path. E.g.
         foo.txt    --> "txt"
         a\b\c.jpg --> "jpg"
         a\b\c     --> ""
         
        Parameters:
        filename - the path of the file
        Returns:
        the extension of filename or "" if none
      • copyFileToDirectory

        @Deprecated
        public static void copyFileToDirectory​(@Nonnull
                                               java.io.File source,
                                               @Nonnull
                                               java.io.File destinationDirectory)
                                        throws java.io.IOException
        Deprecated.
        use org.apache.commons.io.FileUtils.copyFileToDirectory()
        Copy file from source to destination. If destinationDirectory does not exist, it (and any parent directories) will be created. If a file source in destinationDirectory exists, it will be overwritten.
        Parameters:
        source - an existing File to copy
        destinationDirectory - a directory to copy source into
        Throws:
        java.io.FileNotFoundException - if source isn't a normal file
        java.lang.IllegalArgumentException - if destinationDirectory isn't a directory
        java.io.IOException - if source does not exist, the file in destinationDirectory cannot be written to, or an IO error occurs during copying.
      • copyFileToDirectoryIfModified

        private static void copyFileToDirectoryIfModified​(@Nonnull
                                                          java.io.File source,
                                                          @Nonnull
                                                          java.io.File destinationDirectory)
                                                   throws java.io.IOException
        Copy file from source to destination only if source is newer than the target file. If destinationDirectory does not exist, it (and any parent directories) is created. If a file source in destinationDirectory exists, it is overwritten.
        Parameters:
        source - an existing File to copy
        destinationDirectory - a directory to copy source into
        Throws:
        java.io.FileNotFoundException - if source isn't a normal file
        java.lang.IllegalArgumentException - if destinationDirectory isn't a directory
        java.io.IOException - if source does not exist, the file in destinationDirectory cannot be written to, or an IO error occurs during copying
      • copyFile

        @Deprecated
        public static void copyFile​(@Nonnull
                                    java.io.File source,
                                    @Nonnull
                                    java.io.File destination)
                             throws java.io.IOException
        Deprecated.
        use java.nio.Files.copy(source.toPath(), destination.toPath(), CopyOptions.NOFOLLOW_LINKS, CopyOptions.REPLACE_EXISTING)
        Copy file from source to destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.
        Parameters:
        source - an existing non-directory File to copy bytes from
        destination - a non-directory File to write bytes to (possibly overwriting)
        Throws:
        java.io.IOException - if source does not exist, destination cannot be written to, or an IO error occurs during copying
        java.io.FileNotFoundException - if destination is a directory
      • mkdirsFor

        private static void mkdirsFor​(@Nonnull
                                      java.io.File destination)
      • doCopyFile

        private static void doCopyFile​(@Nonnull
                                       java.io.File source,
                                       @Nonnull
                                       java.io.File destination)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • copyFileIfModified

        private static boolean copyFileIfModified​(@Nonnull
                                                  java.io.File source,
                                                  @Nonnull
                                                  java.io.File destination)
                                           throws java.io.IOException
        Copy file from source to destination only if source timestamp is later than the destination timestamp. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.
        Parameters:
        source - An existing non-directory File to copy bytes from.
        destination - A non-directory File to write bytes to (possibly overwriting).
        Returns:
        true if no problem occured
        Throws:
        java.io.IOException - if source does not exist, destination cannot be written to, or an IO error occurs during copying.
      • copyURLToFile

        public static void copyURLToFile​(@Nonnull
                                         java.net.URL source,
                                         @Nonnull
                                         java.io.File destination)
                                  throws java.io.IOException
        Deprecated.
        use java.nio.Files.copy(source.openStream(), destination.toPath(), CopyOptions.REPLACE_EXISTING)
        Copies bytes from the URL source to a file destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.
        Parameters:
        source - A URL to copy bytes from.
        destination - A non-directory File to write bytes to (possibly overwriting).
        Throws:
        java.io.IOException - if
        • source URL cannot be opened
        • destination cannot be written to
        • an IO error occurs during copying
      • copyStreamToFile

        @Deprecated
        private static void copyStreamToFile​(@Nonnull @WillClose
                                             java.io.InputStream source,
                                             @Nonnull
                                             java.io.File destination)
                                      throws java.io.IOException
        Deprecated.
        use java.nio.Files.copy(source, destination.toPath(), CopyOptions.REPLACE_EXISTING)
        Copies bytes from the InputStream source to a file destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.
        Parameters:
        source - An InputStream to copy bytes from. This stream is guaranteed to be closed.
        destination - A non-directory File to write bytes to (possibly overwriting).
        Throws:
        java.io.IOException - if
        • source cannot be opened
        • destination cannot be written to
        • an I/O error occurs during copying
      • normalize

        @Deprecated
        @Nonnull
        public static java.lang.String normalize​(@Nonnull
                                                 java.lang.String path)
        Deprecated.
        use org.apache.commons.io.FileNameUtils.normalize()
        Normalize a path. Eliminates "/../" and "/./" in a string. Returns null if the ..'s went past the root. Eg:
         /foo//               -->     /foo/
         /foo/./              -->     /foo/
         /foo/../bar          -->     /bar
         /foo/../bar/         -->     /bar/
         /foo/../bar/../baz   -->     /baz
         //foo//./bar         -->     /foo/bar
         /../                 -->     null
         
        Parameters:
        path - the path to normalize
        Returns:
        the normalized String, or null if too many ..'s.
      • resolveFile

        @Nonnull
        public static java.io.File resolveFile​(java.io.File baseFile,
                                               @Nonnull
                                               java.lang.String filename)
        Resolve a file filename to it's canonical form. If filename is relative (doesn't start with /), it will be resolved relative to baseFile, otherwise it is treated as a normal root-relative path.
        Parameters:
        baseFile - Where to resolve filename from, if filename is relative.
        filename - absolute or relative file path to resolve
        Returns:
        the canonical File of filename
      • forceDelete

        @Deprecated
        public static void forceDelete​(@Nonnull
                                       java.lang.String file)
                                throws java.io.IOException
        Deprecated.
        use org.apache.commons.io.FileUtils.deleteQuietly()
        Delete a file. If file is directory, delete it and all sub-directories.
        Parameters:
        file - the file path
        Throws:
        java.io.IOException - if any
      • forceDelete

        @Deprecated
        public static void forceDelete​(@Nonnull
                                       java.io.File file)
                                throws java.io.IOException
        Deprecated.
        use org.apache.commons.io.FileUtils.deleteQuietly()
        Delete a file. If file is directory, delete it and all sub-directories.
        Parameters:
        file - a file
        Throws:
        java.io.IOException - if any
      • delete

        @Deprecated
        public static void delete​(@Nonnull
                                  java.io.File file)
                           throws java.io.IOException
        Deprecated.
        use java.nio.files.Files.delete(file.toPath())
        Deletes a file.
        Parameters:
        file - the file to delete
        Throws:
        java.io.IOException - if the file cannot be deleted
      • deleteLegacyStyle

        @Deprecated
        public static boolean deleteLegacyStyle​(@Nonnull
                                                java.io.File file)
        Deprecated.
        use java.nio.files.Files.delete(file.toPath())
        Parameters:
        file - the file
        Returns:
        true / false
      • deleteFile

        private static boolean deleteFile​(@Nonnull
                                          java.io.File file)
                                   throws java.io.IOException
        Accommodate Windows bug encountered in both Sun and IBM JDKs. Others possible. If the delete does not work, call System.gc(), wait a little and try again.
        Parameters:
        file - a file
        Throws:
        java.io.IOException - if any
      • forceMkdir

        public static void forceMkdir​(@Nonnull
                                      java.io.File file)
                               throws java.io.IOException
        Make a directory.
        Parameters:
        file - not null
        Throws:
        java.io.IOException - if a file already exists with the specified name or the directory is unable to be created
        java.lang.IllegalArgumentException - if the file contains illegal Windows characters under Windows OS.
        See Also:
        INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
      • deleteDirectory

        @Deprecated
        public static void deleteDirectory​(@Nonnull
                                           java.lang.String directory)
                                    throws java.io.IOException
        Deprecated.
        use org.apache.commons.io.FileUtils.deleteDirectory()
        Recursively delete a directory.
        Parameters:
        directory - a directory
        Throws:
        java.io.IOException - if any
      • deleteDirectory

        @Deprecated
        public static void deleteDirectory​(@Nonnull
                                           java.io.File directory)
                                    throws java.io.IOException
        Deprecated.
        use org.apache.commons.io.FileUtils.deleteDirectory()
        Recursively delete a directory.
        Parameters:
        directory - a directory
        Throws:
        java.io.IOException - if any
      • cleanDirectory

        @Deprecated
        public static void cleanDirectory​(@Nonnull
                                          java.io.File directory)
                                   throws java.io.IOException
        Deprecated.
        use org.apache.commons.io.FileUtils.cleanDirectory()
        Remove all files from a directory without deleting it.
        Parameters:
        directory - a directory
        Throws:
        java.io.IOException - if any. This can leave cleaning in a half-finished state where some but not all files have been deleted.
      • sizeOfDirectory

        @Deprecated
        public static long sizeOfDirectory​(@Nonnull
                                           java.lang.String directory)
        Deprecated.
        use org.apache.commons.io.FileUtils.sizeOf()
        Recursively count size of a directory.
        Parameters:
        directory - a directory
        Returns:
        size of directory in bytes
      • sizeOfDirectory

        @Deprecated
        public static long sizeOfDirectory​(@Nonnull
                                           java.io.File directory)
        Deprecated.
        use org.apache.commons.io.FileUtils.sizeOf()
        Recursively count size of a directory.
        Parameters:
        directory - a directory
        Returns:
        size of directory in bytes
      • getFiles

        @Nonnull
        public static java.util.List<java.io.File> getFiles​(@Nonnull
                                                            java.io.File directory,
                                                            @Nullable
                                                            java.lang.String includes,
                                                            @Nullable
                                                            java.lang.String excludes)
                                                     throws java.io.IOException
        Return the files contained in the directory, using inclusion and exclusion Ant patterns, including the directory name in each of the files
        Parameters:
        directory - the directory to scan
        includes - the Ant includes pattern, comma separated
        excludes - the Ant excludes pattern, comma separated
        Returns:
        a list of File objects
        Throws:
        java.io.IOException - in case of failure.
        See Also:
        getFileNames(File, String, String, boolean)
      • getFiles

        @Nonnull
        public static java.util.List<java.io.File> getFiles​(@Nonnull
                                                            java.io.File directory,
                                                            @Nullable
                                                            java.lang.String includes,
                                                            @Nullable
                                                            java.lang.String excludes,
                                                            boolean includeBasedir)
                                                     throws java.io.IOException
        Return the files contained in the directory, using inclusion and exclusion Ant patterns
        Parameters:
        directory - the directory to scan
        includes - the includes pattern, comma separated
        excludes - the excludes pattern, comma separated
        includeBasedir - true to include the base dir in each file
        Returns:
        a list of File objects
        Throws:
        java.io.IOException - in case of failure.
        See Also:
        getFileNames(File, String, String, boolean)
      • getFileNames

        @Nonnull
        public static java.util.List<java.lang.String> getFileNames​(@Nonnull
                                                                    java.io.File directory,
                                                                    @Nullable
                                                                    java.lang.String includes,
                                                                    @Nullable
                                                                    java.lang.String excludes,
                                                                    boolean includeBasedir)
                                                             throws java.io.IOException
        Return a list of files as String depending options. This method use case sensitive file name.
        Parameters:
        directory - the directory to scan
        includes - the Ant includes pattern, comma separated
        excludes - the Ant excludes pattern, comma separated
        includeBasedir - true to include the base directory in each String of file
        Returns:
        a list of file names
        Throws:
        java.io.IOException - in case of failure
      • getFileNames

        @Nonnull
        private static java.util.List<java.lang.String> getFileNames​(@Nonnull
                                                                     java.io.File directory,
                                                                     @Nullable
                                                                     java.lang.String includes,
                                                                     @Nullable
                                                                     java.lang.String excludes,
                                                                     boolean includeBasedir,
                                                                     boolean isCaseSensitive)
                                                              throws java.io.IOException
        Return a list of files as String depending options.
        Parameters:
        directory - the directory to scan
        includes - the Ant includes pattern, comma separated
        excludes - the Ant excludes pattern, comma separated
        includeBasedir - true to include the base dir in each String of file
        isCaseSensitive - true if case sensitive
        Returns:
        a list of files as String
        Throws:
        java.io.IOException
      • getDirectoryNames

        @Nonnull
        public static java.util.List<java.lang.String> getDirectoryNames​(@Nonnull
                                                                         java.io.File directory,
                                                                         @Nullable
                                                                         java.lang.String includes,
                                                                         @Nullable
                                                                         java.lang.String excludes,
                                                                         boolean includeBasedir)
                                                                  throws java.io.IOException
        Return a list of directories as String depending options. This method use case sensitive file name.
        Parameters:
        directory - the directory to scan
        includes - the Ant includes pattern, comma separated
        excludes - the Ant excludes pattern, comma separated
        includeBasedir - true to include the base dir in each String of file
        Returns:
        a list of directories as String
        Throws:
        java.io.IOException - in case of failure.
      • getDirectoryNames

        @Nonnull
        public static java.util.List<java.lang.String> getDirectoryNames​(@Nonnull
                                                                         java.io.File directory,
                                                                         @Nullable
                                                                         java.lang.String includes,
                                                                         @Nullable
                                                                         java.lang.String excludes,
                                                                         boolean includeBasedir,
                                                                         boolean isCaseSensitive)
                                                                  throws java.io.IOException
        Return a list of directories as Strings.
        Parameters:
        directory - the directory to scan
        includes - the Ant includes pattern, comma separated
        excludes - the Ant excludes pattern, comma separated
        includeBasedir - true to include the base directory in each String of file
        isCaseSensitive - true if case sensitive
        Returns:
        a list of directories as String
        Throws:
        java.io.IOException - in case of failure
      • getFileAndDirectoryNames

        @Nonnull
        public static java.util.List<java.lang.String> getFileAndDirectoryNames​(java.io.File directory,
                                                                                @Nullable
                                                                                java.lang.String includes,
                                                                                @Nullable
                                                                                java.lang.String excludes,
                                                                                boolean includeBasedir,
                                                                                boolean isCaseSensitive,
                                                                                boolean getFiles,
                                                                                boolean getDirectories)
        Return a list of file names as Strings.
        Parameters:
        directory - the directory to scan
        includes - the Ant includes pattern, comma separated
        excludes - the Ant excludes pattern, comma separated
        includeBasedir - true to include the base directory in each String of file
        isCaseSensitive - true if case sensitive
        getFiles - true to include regular files
        getDirectories - true to include directories
        Returns:
        a list of file names
      • copyDirectory

        public static void copyDirectory​(@Nonnull
                                         java.io.File sourceDirectory,
                                         @Nonnull
                                         java.io.File destinationDirectory)
                                  throws java.io.IOException
        Copy the contents of a directory into another one.
        Parameters:
        sourceDirectory - the source directory
        destinationDirectory - the target directory
        Throws:
        java.io.IOException - if any
      • copyDirectory

        public static void copyDirectory​(@Nonnull
                                         java.io.File sourceDirectory,
                                         @Nonnull
                                         java.io.File destinationDirectory,
                                         @Nullable
                                         java.lang.String includes,
                                         @Nullable
                                         java.lang.String excludes)
                                  throws java.io.IOException
        Copy the contents of a directory into another one.
        Parameters:
        sourceDirectory - the source directory
        destinationDirectory - the target directory
        includes - Ant include pattern
        excludes - Ant exclude pattern
        Throws:
        java.io.IOException - if any
        See Also:
        getFiles(File, String, String)
      • copyDirectoryStructure

        @Deprecated
        public static void copyDirectoryStructure​(@Nonnull
                                                  java.io.File sourceDirectory,
                                                  @Nonnull
                                                  java.io.File destinationDirectory)
                                           throws java.io.IOException
        Deprecated.
        use org.apache.commons.io.FileUtils.copyDirectory()
        Copies an entire directory structure.

        Note:

        • It will include empty directories.
        • The sourceDirectory must exist.
        Parameters:
        sourceDirectory - the source dir
        destinationDirectory - the target dir
        Throws:
        java.io.IOException - if any
      • copyDirectoryStructure

        private static void copyDirectoryStructure​(@Nonnull
                                                   java.io.File sourceDirectory,
                                                   @Nonnull
                                                   java.io.File destinationDirectory,
                                                   java.io.File rootDestinationDirectory,
                                                   boolean onlyModifiedFiles)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • rename

        @Deprecated
        public static void rename​(@Nonnull
                                  java.io.File from,
                                  @Nonnull
                                  java.io.File to)
                           throws java.io.IOException
        Deprecated.
        use java.nio.Files.move()
        Renames a file, even if that involves crossing file system boundaries.

        This will remove to (if it exists), ensure that to's parent directory exists and move from, which involves deleting from as well.

        Parameters:
        from - the file to move
        to - the new file name
        Throws:
        java.io.IOException - if anything bad happens during this process. Note that to may have been deleted already when this happens.
      • createTempFile

        @Deprecated
        public static java.io.File createTempFile​(@Nonnull
                                                  java.lang.String prefix,
                                                  @Nonnull
                                                  java.lang.String suffix,
                                                  @Nullable
                                                  java.io.File parentDir)
        Deprecated.
        use java.nio.Files.createTempFile()
        Create a temporary file in a given directory.

        The file denoted by the returned abstract pathname did not exist before this method was invoked, any subsequent invocation of this method will yield a different file name.

        The filename is prefixNNNNNsuffix where NNNN is a random number

        This method is different to File.createTempFile(String, String, File) as it doesn't create the file itself. It uses the location pointed to by java.io.tmpdir when the parentDir attribute is null.

        To automatically delete the file created by this method, use the File.deleteOnExit() method.

        Parameters:
        prefix - prefix before the random number
        suffix - file extension; include the '.'
        parentDir - directory to create the temporary file in -java.io.tmpdir used if not specified
        Returns:
        a File reference to the new temporary file.
      • positiveRandom

        private static int positiveRandom​(java.util.Random rand)
      • copyFile

        public static void copyFile​(@Nonnull
                                    java.io.File from,
                                    @Nonnull
                                    java.io.File to,
                                    @Nullable
                                    java.lang.String encoding,
                                    @Nullable
                                    FileUtils.FilterWrapper... wrappers)
                             throws java.io.IOException
        If wrappers is null or empty, the file will be copied only if to.lastModified() < from.lastModified()
        Parameters:
        from - the file to copy
        to - the destination file
        encoding - the file output encoding (only if wrappers is not empty)
        wrappers - array of FileUtils.FilterWrapper
        Throws:
        java.io.IOException - if an IO error occurs during copying or filtering
      • copyFile

        public static void copyFile​(@Nonnull
                                    java.io.File from,
                                    @Nonnull
                                    java.io.File to,
                                    @Nullable
                                    java.lang.String encoding,
                                    @Nullable
                                    FileUtils.FilterWrapper[] wrappers,
                                    boolean overwrite)
                             throws java.io.IOException
        If wrappers is null or empty, the file will be copy only if to.lastModified() < from.lastModified() or if overwrite is true
        Parameters:
        from - the file to copy
        to - the destination file
        encoding - the file output encoding (only if wrappers is not empty)
        wrappers - array of FileUtils.FilterWrapper
        overwrite - if true and wrappers is null or empty, the file will be copied even if to.lastModified() < from.lastModified()
        Throws:
        java.io.IOException - if an IO error occurs during copying or filtering
      • copyFilePermissions

        private static void copyFilePermissions​(@Nonnull
                                                java.io.File source,
                                                @Nonnull
                                                java.io.File destination)
                                         throws java.io.IOException
        Attempts to copy file permissions from the source to the destination file. Initially attempts to copy posix file permissions, assuming that the files are both on posix filesystems. If the initial attempts fail then a second attempt using less precise permissions model. Note that permissions are copied on a best-efforts basis, failure to copy permissions will not result in an exception.
        Parameters:
        source - the file to copy permissions from.
        destination - the file to copy permissions to.
        Throws:
        java.io.IOException
      • loadFile

        @Deprecated
        @Nonnull
        public static java.util.List<java.lang.String> loadFile​(@Nonnull
                                                                java.io.File file)
                                                         throws java.io.IOException
        Deprecated.
        assumes the platform default character set
        Note: the file content is read with platform encoding.
        Parameters:
        file - the file
        Returns:
        a List containing every every line not starting with # and not empty
        Throws:
        java.io.IOException - if any
      • charset

        private static java.nio.charset.Charset charset​(java.lang.String encoding)
        Returns the named charset or the default charset.
        Parameters:
        encoding - the name or alias of the charset, null or empty
        Returns:
        A charset object for the named or default charset.
      • isValidWindowsFileName

        private static boolean isValidWindowsFileName​(@Nonnull
                                                      java.io.File f)
        For Windows OS, check if the file name contains any of the following characters: ":", "*", "?", "\"", "<", ">", "|"
        Parameters:
        f - not null file
        Returns:
        false if the file path contains any of forbidden Windows characters, true if the Os is not Windows or if the file path respect the Windows constraints.
        See Also:
        INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
      • isSymbolicLink

        @Deprecated
        public static boolean isSymbolicLink​(@Nonnull
                                             java.io.File file)
                                      throws java.io.IOException
        Deprecated.
        use java.nio.file.Files.isSymbolicLink(file.toPath())
        Checks whether a given file is a symbolic link.
        Parameters:
        file - the file to check
        Returns:
        true if symbolic link false otherwise.
        Throws:
        java.io.IOException - in case of failure.
      • isSymbolicLinkForSure

        @Deprecated
        public static boolean isSymbolicLinkForSure​(@Nonnull
                                                    java.io.File file)
                                             throws java.io.IOException
        Deprecated.
        use java.nio.file.Files.isSymbolicLink(file.toPath())
        Checks whether a given file is a symbolic link.
        Parameters:
        file - the file to check
        Returns:
        true if and only if we reliably can say this is a symlink
        Throws:
        java.io.IOException - in case of failure
      • createSymbolicLink

        @Nonnull
        public static java.io.File createSymbolicLink​(@Nonnull
                                                      java.io.File symlink,
                                                      @Nonnull
                                                      java.io.File target)
                                               throws java.io.IOException
        Create a new symbolic link, possibly replacing an existing symbolic link.
        Parameters:
        symlink - the link name
        target - the target
        Returns:
        the linked file
        Throws:
        java.io.IOException - in case of an error