Class FileUtil


  • public class FileUtil
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void checkFileExists​(java.io.File file)  
      static void copy​(java.io.File srcFile, java.io.File dstFile)  
      static java.io.File copyToTemp​(java.io.File srcFile)  
      static java.io.File createTempDir​(java.lang.String name)  
      static java.io.File createTempDir​(java.lang.String name, boolean reuse)  
      static java.io.File createTempFile​(java.lang.String name)  
      static java.io.File createTempFile​(java.lang.String name, java.lang.String extension)  
      static void deleteDirectory​(java.lang.String dir)
      static void deleteDirectory​(java.nio.file.Path path)
      WARNIING: Use with caution! Deletes all contents and sub-directories of the specified path.
      static java.lang.String getAbsolutePath​(java.lang.String basePath, java.lang.String relativePath)  
      static java.nio.file.Path getCurrentDirectory()  
      static java.io.File getDirectoryOf​(java.lang.Class<?> clazz)  
      static java.io.File getDirectoryStandalone​(java.lang.String dir, java.lang.Class<?> relativeTo)
      Convenience method for copying all files from the workspace / JAR path (relative to the class) to temp folder.
      static java.lang.String getExtension​(java.lang.String filename)
      Copied from @linkplain{https://stackoverflow.com/a/3571239/5870336}
      static java.lang.String getFileContents​(java.io.File file)  
      static java.util.Collection<java.lang.String> getFileLineContents​(java.io.File file)  
      static java.lang.String getFileName​(java.lang.String path)  
      static java.lang.String getFileName​(java.lang.String path, boolean includeExtension)  
      static java.io.File getFileStandalone​(java.lang.String name, java.lang.Class<?> relativeTo)
      Gets a file stored as a resource in a jar.
      static java.io.File getFileURL​(java.lang.String name, java.lang.Class<?> relativeTo)
      The getFile method of old, before we tried to fix it to work in JAR files.
      static java.lang.String getPath​(java.lang.String name, java.lang.Class<?> relativeTo)  
      static java.nio.file.Path getStandalonePath​(java.lang.String dir, java.lang.Class<?> relativeTo)  
      static boolean isInJarFile()  
      static boolean isInJarFile​(java.lang.Class<?> clazz)  
      static java.util.Set<java.lang.String> listFilesAsSet​(java.io.File fileExpected)  
      static java.util.Map<java.nio.file.Path,​byte[]> readDirectory​(java.lang.String dir)
      static java.util.Map<java.nio.file.Path,​byte[]> readDirectory​(java.nio.file.Path dir)
      Reads entire directory recursively, mapping the contents of each file as a string to its path.
      static java.lang.String removeExtension​(java.lang.String filename)  
      static java.lang.String replaceExtension​(java.lang.String filename, java.lang.String newExtension)  
      static boolean sameContents​(java.io.File fileExpected, java.io.File fileActual, java.util.Set<java.lang.String> ignoreFilenames)
      We implement our own comparison algorithm here, so we don't need Eclipse Compare to compute differences, but rather only to show them in the UI.
      static boolean sameContents​(java.io.InputStream isExpected, java.io.InputStream isActual)  
      static void setFileContents​(java.lang.String str, java.io.File file)  
      static java.nio.file.Path stringToPath​(java.lang.String dir)  
      • Methods inherited from class java.lang.Object

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

      • getCurrentDirectory

        public static java.nio.file.Path getCurrentDirectory()
      • setFileContents

        public static void setFileContents​(java.lang.String str,
                                           java.io.File file)
                                    throws java.lang.Exception
        Throws:
        java.lang.Exception
      • replaceExtension

        public static java.lang.String replaceExtension​(java.lang.String filename,
                                                        java.lang.String newExtension)
      • removeExtension

        public static java.lang.String removeExtension​(java.lang.String filename)
      • getFileName

        public static java.lang.String getFileName​(java.lang.String path)
      • getFileName

        public static java.lang.String getFileName​(java.lang.String path,
                                                   boolean includeExtension)
      • getExtension

        public static java.lang.String getExtension​(java.lang.String filename)
        Copied from @linkplain{https://stackoverflow.com/a/3571239/5870336}
        Parameters:
        filename -
        Returns:
        Since:
        1.6
      • getFileContents

        public static java.lang.String getFileContents​(java.io.File file)
                                                throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getFileLineContents

        public static java.util.Collection<java.lang.String> getFileLineContents​(java.io.File file)
                                                                          throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getAbsolutePath

        public static java.lang.String getAbsolutePath​(java.lang.String basePath,
                                                       java.lang.String relativePath)
      • isInJarFile

        public static boolean isInJarFile()
        Returns:
        Since:
        1.6
      • isInJarFile

        public static boolean isInJarFile​(java.lang.Class<?> clazz)
        Parameters:
        clazz -
        Returns:
        Since:
        1.6
      • getStandalonePath

        public static java.nio.file.Path getStandalonePath​(java.lang.String dir,
                                                           java.lang.Class<?> relativeTo)
                                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • getDirectoryStandalone

        public static java.io.File getDirectoryStandalone​(java.lang.String dir,
                                                          java.lang.Class<?> relativeTo)
                                                   throws java.io.IOException
        Convenience method for copying all files from the workspace / JAR path (relative to the class) to temp folder. Used for tests.
        Parameters:
        dir -
        relativeTo -
        Returns:
        Throws:
        java.io.IOException
        java.net.URISyntaxException
        Since:
        1.6
        See Also:
        getFileStandalone(String, Class)
      • getFileStandalone

        public static java.io.File getFileStandalone​(java.lang.String name,
                                                     java.lang.Class<?> relativeTo)
                                              throws java.io.IOException
        Gets a file stored as a resource in a jar. Since not all users of the file can read from inside jars, we get the file as a stream and create a temp file with its contents.
        Parameters:
        name -
        relativeTo -
        Returns:
        Throws:
        java.io.IOException
        Since:
        1.6
      • getFileURL

        public static java.io.File getFileURL​(java.lang.String name,
                                              java.lang.Class<?> relativeTo)
        The getFile method of old, before we tried to fix it to work in JAR files.
        Parameters:
        name -
        relativeTo -
        Returns:
        Since:
        1.6
      • getDirectoryOf

        public static java.io.File getDirectoryOf​(java.lang.Class<?> clazz)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • getPath

        public static java.lang.String getPath​(java.lang.String name,
                                               java.lang.Class<?> relativeTo)
                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • checkFileExists

        public static void checkFileExists​(java.io.File file)
                                    throws java.io.FileNotFoundException
        Throws:
        java.io.FileNotFoundException
      • copyToTemp

        public static java.io.File copyToTemp​(java.io.File srcFile)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • copy

        public static void copy​(java.io.File srcFile,
                                java.io.File dstFile)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • listFilesAsSet

        public static java.util.Set<java.lang.String> listFilesAsSet​(java.io.File fileExpected)
      • sameContents

        public static boolean sameContents​(java.io.File fileExpected,
                                           java.io.File fileActual,
                                           java.util.Set<java.lang.String> ignoreFilenames)
                                    throws java.io.IOException
        We implement our own comparison algorithm here, so we don't need Eclipse Compare to compute differences, but rather only to show them in the UI.
        Throws:
        java.io.IOException
      • sameContents

        public static boolean sameContents​(java.io.InputStream isExpected,
                                           java.io.InputStream isActual)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • stringToPath

        public static java.nio.file.Path stringToPath​(java.lang.String dir)
        Parameters:
        dir -
        Returns:
        Since:
        1.6
      • deleteDirectory

        public static void deleteDirectory​(java.lang.String dir)
                                    throws java.io.IOException
        Parameters:
        path -
        Throws:
        java.io.IOException
        Since:
        1.6
      • deleteDirectory

        public static void deleteDirectory​(java.nio.file.Path path)
                                    throws java.io.IOException
        WARNIING: Use with caution! Deletes all contents and sub-directories of the specified path.
        Parameters:
        dir - The absolute path to the directory.
        Throws:
        java.io.IOException
        Since:
        1.6
      • readDirectory

        public static java.util.Map<java.nio.file.Path,​byte[]> readDirectory​(java.lang.String dir)
                                                                            throws java.io.IOException
        Parameters:
        dir -
        Returns:
        Throws:
        java.io.IOException
        Since:
        1.6
      • readDirectory

        public static java.util.Map<java.nio.file.Path,​byte[]> readDirectory​(java.nio.file.Path dir)
                                                                            throws java.io.IOException
        Reads entire directory recursively, mapping the contents of each file as a string to its path.
        Parameters:
        dir - The root directory.
        Returns:
        The contents of each file in the directory and its subdirectories.
        Throws:
        java.io.IOException
        Since:
        1.6
      • createTempFile

        public static java.io.File createTempFile​(java.lang.String name)
        Parameters:
        name -
        Returns:
        Since:
        1.6
      • createTempFile

        public static java.io.File createTempFile​(java.lang.String name,
                                                  java.lang.String extension)
        Parameters:
        name -
        extension -
        Returns:
        Since:
        1.6
      • createTempDir

        public static java.io.File createTempDir​(java.lang.String name)
        Parameters:
        name -
        Returns:
        Since:
        1.6
      • createTempDir

        public static java.io.File createTempDir​(java.lang.String name,
                                                 boolean reuse)
        Parameters:
        name -
        reuse -
        Returns:
        Since:
        1.6