Class FileUtil


  • public class FileUtil
    extends Object
    • Method Detail

      • getCurrentDirectory

        public static Path getCurrentDirectory()
      • replaceExtension

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

        public static String removeExtension​(String filename)
      • getFileName

        public static String getFileName​(String path)
      • getFileName

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

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

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

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

        public static boolean isInJarFile​(Class<?> clazz)
        Parameters:
        clazz -
        Returns:
        Since:
        1.6
      • getFileStandalone

        public static File getFileStandalone​(String name,
                                             Class<?> relativeTo)
                                      throws 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:
        IOException
        Since:
        1.6
      • getFileURL

        public static File getFileURL​(String name,
                                      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
      • listFilesAsSet

        public static Set<String> listFilesAsSet​(File fileExpected)
      • sameContents

        public static boolean sameContents​(File fileExpected,
                                           File fileActual,
                                           Set<String> ignoreFilenames)
                                    throws 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:
        IOException
      • stringToPath

        public static Path stringToPath​(String dir)
        Parameters:
        dir -
        Returns:
        Since:
        1.6
      • deleteDirectory

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

        public static Map<Path,​byte[]> readDirectory​(Path dir)
                                                    throws 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:
        IOException
        Since:
        1.6
      • createTempFile

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

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

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

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