Class FileUtil


  • public final class FileUtil
    extends java.lang.Object
    Utility class used to deal with file related operations, like copy, full reading, symlink, ...
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String concat​(java.lang.String dir, java.lang.String file)  
      static boolean copy​(java.io.File src, java.io.File dest, CopyProgressListener l)  
      static boolean copy​(java.io.File src, java.io.File dest, CopyProgressListener l, boolean overwrite)  
      static void copy​(java.io.File src, java.net.URL dest, CopyProgressListener listener, TimeoutConstraint timeoutConstraint)  
      static void copy​(java.io.InputStream src, java.io.File dest, CopyProgressListener l)  
      static void copy​(java.io.InputStream src, java.io.OutputStream dest, CopyProgressListener l)  
      static void copy​(java.io.InputStream src, java.io.OutputStream dest, CopyProgressListener l, boolean autoClose)  
      static void copy​(java.net.URL src, java.io.File dest, CopyProgressListener listener, TimeoutConstraint timeoutConstraint)  
      static boolean deepCopy​(java.io.File src, java.io.File dest, CopyProgressListener l, boolean overwrite)  
      static boolean forceDelete​(java.io.File file)
      Recursively delete file
      static long getFileLength​(java.io.File file)
      Get the length of the file, or the sum of the children lengths if it is a directory
      static java.util.List<java.io.File> getPathFiles​(java.io.File root, java.io.File file)
      Returns a list of Files composed of all directories being parent of file and child of root + file and root themselves.
      static java.util.Collection<java.io.File> listAll​(java.io.File dir, java.util.Collection<java.lang.String> ignore)  
      static java.io.File normalize​(java.lang.String path)
      "Normalize" the given absolute path.
      static boolean prepareCopy​(java.io.File src, java.io.File dest, boolean overwrite)  
      static java.lang.String readEntirely​(java.io.BufferedReader in)
      Reads the whole BufferedReader line by line, using \n as line separator for each line.
      static java.lang.String readEntirely​(java.io.File f)
      Reads the entire content of the file and returns it as a String.
      static java.lang.String readEntirely​(java.io.InputStream is)
      Reads the entire content of the InputStream and returns it as a String.
      static java.io.File resolveFile​(java.io.File file, java.lang.String filename)  
      static boolean symlink​(java.io.File target, java.io.File link, boolean overwrite)
      Creates a symbolic link at link whose target will be the target.
      static java.io.InputStream unwrapPack200​(java.io.InputStream packed)  
      • Methods inherited from class java.lang.Object

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

      • symlink

        public static boolean symlink​(java.io.File target,
                                      java.io.File link,
                                      boolean overwrite)
                               throws java.io.IOException
        Creates a symbolic link at link whose target will be the target. Depending on the underlying filesystem, this method may not always be able to create a symbolic link, in which case this method returns false.
        Parameters:
        target - The File which will be the target of the symlink being created
        link - The path to the symlink that needs to be created
        overwrite - true if any existing file at link has to be overwritten. False otherwise
        Returns:
        Returns true if the symlink was successfully created. Returns false if the symlink could not be created
        Throws:
        java.io.IOException - if Files.createSymbolicLink(java.nio.file.Path, java.nio.file.Path, java.nio.file.attribute.FileAttribute<?>...) fails
      • copy

        public static boolean copy​(java.io.File src,
                                   java.io.File dest,
                                   CopyProgressListener l)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • prepareCopy

        public static boolean prepareCopy​(java.io.File src,
                                          java.io.File dest,
                                          boolean overwrite)
                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • copy

        public static boolean copy​(java.io.File src,
                                   java.io.File dest,
                                   CopyProgressListener l,
                                   boolean overwrite)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • deepCopy

        public static boolean deepCopy​(java.io.File src,
                                       java.io.File dest,
                                       CopyProgressListener l,
                                       boolean overwrite)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • copy

        public static void copy​(java.net.URL src,
                                java.io.File dest,
                                CopyProgressListener listener,
                                TimeoutConstraint timeoutConstraint)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • copy

        public static void copy​(java.io.File src,
                                java.net.URL dest,
                                CopyProgressListener listener,
                                TimeoutConstraint timeoutConstraint)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • copy

        public static void copy​(java.io.InputStream src,
                                java.io.File dest,
                                CopyProgressListener l)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • copy

        public static void copy​(java.io.InputStream src,
                                java.io.OutputStream dest,
                                CopyProgressListener l)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • copy

        public static void copy​(java.io.InputStream src,
                                java.io.OutputStream dest,
                                CopyProgressListener l,
                                boolean autoClose)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • readEntirely

        public static java.lang.String readEntirely​(java.io.BufferedReader in)
                                             throws java.io.IOException
        Reads the whole BufferedReader line by line, using \n as line separator for each line.

        Note that this method will add a final \n to the last line even though there is no new line character at the end of last line in the original reader.

        The BufferedReader is closed when this method returns.

        Parameters:
        in - the BufferedReader to read from
        Returns:
        a String with the whole content read from the BufferedReader
        Throws:
        java.io.IOException - if an IO problems occur during reading
      • readEntirely

        public static java.lang.String readEntirely​(java.io.File f)
                                             throws java.io.IOException
        Reads the entire content of the file and returns it as a String.
        Parameters:
        f - the file to read from
        Returns:
        a String with the file content
        Throws:
        java.io.IOException - if an IO problems occurs during reading
      • readEntirely

        public static java.lang.String readEntirely​(java.io.InputStream is)
                                             throws java.io.IOException
        Reads the entire content of the InputStream and returns it as a String.

        The input stream is closed when this method returns.

        Parameters:
        is - the InputStream to read from
        Returns:
        a String with the input stream content
        Throws:
        java.io.IOException - if an IO problems occurs during reading
      • concat

        public static java.lang.String concat​(java.lang.String dir,
                                              java.lang.String file)
      • forceDelete

        public static boolean forceDelete​(java.io.File file)
        Recursively delete file
        Parameters:
        file - the file to delete
        Returns:
        true if the deletion completed successfully (ie if the file does not exist on the filesystem after this call), false if a deletion was not performed successfully.
      • getPathFiles

        public static java.util.List<java.io.File> getPathFiles​(java.io.File root,
                                                                java.io.File file)
        Returns a list of Files composed of all directories being parent of file and child of root + file and root themselves. Example: getPathFiles(new File("test"), new File("test/dir1/dir2/file.txt")) => {new File("test/dir1"), new File("test/dir1/dir2"), new File("test/dir1/dir2/file.txt") } Note that if root is not an ancestor of file, or if root is null, all directories from the file system root will be returned.
        Parameters:
        root - File
        file - File
        Returns:
        List<File>
      • listAll

        public static java.util.Collection<java.io.File> listAll​(java.io.File dir,
                                                                 java.util.Collection<java.lang.String> ignore)
        Parameters:
        dir - The directory from which all files, including files in subdirectory) are extracted.
        ignore - a Collection of filenames which must be excluded from listing
        Returns:
        a collection containing all the files of the given directory and it's subdirectories, recursively.
      • resolveFile

        public static java.io.File resolveFile​(java.io.File file,
                                               java.lang.String filename)
      • normalize

        public static java.io.File normalize​(java.lang.String path)
        "Normalize" the given absolute path.

        This includes:

        • Uppercase the drive letter if there is one.
        • Remove redundant slashes after the drive spec.
        • Resolve all ./, .\, ../ and ..\ sequences.
        • DOS style paths that start with a drive letter will have \ as the separator.
        Unlike File.getCanonicalPath() this method specifically does not resolve symbolic links.
        Parameters:
        path - the path to be normalized.
        Returns:
        the normalized version of the path.
        Throws:
        java.lang.NullPointerException - if path is null.
      • getFileLength

        public static long getFileLength​(java.io.File file)
        Get the length of the file, or the sum of the children lengths if it is a directory
        Parameters:
        file - File
        Returns:
        long
      • unwrapPack200

        public static java.io.InputStream unwrapPack200​(java.io.InputStream packed)
                                                 throws java.io.IOException
        Throws:
        java.io.IOException