Class Files


  • public class Files
    extends java.lang.Object
    Utility methods related to files.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Files()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      private static java.io.UncheckedIOException cannotCreateNewFile​(java.lang.String path, java.lang.Exception cause)  
      private static java.io.UncheckedIOException cannotCreateNewFile​(java.lang.String path, java.lang.String reason)  
      private static java.io.UncheckedIOException cannotCreateNewFile​(java.lang.String path, java.lang.String reason, java.lang.Exception cause)  
      private static void checkArgumentCharsetIsSupported​(java.lang.String charsetName)  
      static java.lang.String contentOf​(java.io.File file, java.lang.String charsetName)
      Loads the text content of a file into a character string.
      static java.lang.String contentOf​(java.io.File file, java.nio.charset.Charset charset)
      Loads the text content of a file into a character string.
      private static java.io.File createFileIfPathIsNotANonEmptyDirectory​(java.lang.String path)  
      static java.io.File currentFolder()
      Returns the current directory.
      static void delete​(java.io.File file)
      Deprecated.
      use https://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/FileUtils.html#forceDelete-java.io.File- instead
      private static java.util.List<java.lang.String> fileNamesIn​(java.io.File dir, boolean recurse)
      Returns the names of the files inside the specified directory.
      static java.util.List<java.lang.String> fileNamesIn​(java.lang.String dirName, boolean recurse)
      Returns the names of the files inside the specified directory.
      static java.util.Optional<java.lang.String> getFileNameExtension​(java.lang.String fileName)  
      static java.util.List<java.lang.String> linesOf​(java.io.File file, java.lang.String charsetName)
      Loads the text content of a file into a list of strings, each string corresponding to a line.
      static java.util.List<java.lang.String> linesOf​(java.io.File file, java.nio.charset.Charset charset)
      Loads the text content of a file into a list of strings, each string corresponding to a line.
      static java.io.File newFile​(java.lang.String path)
      Creates a new file using the given path.
      static java.io.File newFolder​(java.lang.String path)
      Creates a new directory using the given path.
      static java.io.File newTemporaryFile()
      Creates a new file in the system's temporary directory.
      static java.io.File newTemporaryFolder()
      Creates a new directory in the system's temporary directory.
      static java.io.File temporaryFolder()
      Deprecated.
      Use either TempDir or TemporaryFolder
      static java.lang.String temporaryFolderPath()
      Returns the path of the system's temporary directory.
      • Methods inherited from class java.lang.Object

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

      • Files

        private Files()
    • Method Detail

      • fileNamesIn

        public static java.util.List<java.lang.String> fileNamesIn​(java.lang.String dirName,
                                                                   boolean recurse)
        Returns the names of the files inside the specified directory.
        Parameters:
        dirName - the name of the directory to start the search from.
        recurse - if true, we will look in subdirectories.
        Returns:
        the names of the files inside the specified directory.
        Throws:
        java.lang.IllegalArgumentException - if the given directory name does not point to an existing directory.
      • fileNamesIn

        private static java.util.List<java.lang.String> fileNamesIn​(java.io.File dir,
                                                                    boolean recurse)
        Returns the names of the files inside the specified directory.
        Parameters:
        dir - the name of the directory to start the search from.
        recurse - if true, we will look in subdirectories.
        Returns:
        the names of the files inside the specified directory.
      • temporaryFolder

        @Deprecated
        public static java.io.File temporaryFolder()
        Deprecated.
        Use either TempDir or TemporaryFolder
        Returns the system's temporary directory.
        Returns:
        the system's temporary directory.
        Throws:
        java.lang.RuntimeException - if this method cannot find or create the system's temporary directory.
      • temporaryFolderPath

        public static java.lang.String temporaryFolderPath()
        Returns the path of the system's temporary directory. This method appends the system's file separator at the end of the path.
        Returns:
        the path of the system's temporary directory.
      • newTemporaryFile

        public static java.io.File newTemporaryFile()
        Creates a new file in the system's temporary directory. The name of the file will be the result of:
         concat(UUID.randomUUID().toString(), ".txt");
        Returns:
        the created file.
      • newTemporaryFolder

        public static java.io.File newTemporaryFolder()
        Creates a new directory in the system's temporary directory. The name of the directory will be the result of:
         UUID.randomUUID().toString();
        Returns:
        the created file.
      • newFile

        public static java.io.File newFile​(java.lang.String path)
        Creates a new file using the given path.
        Parameters:
        path - the path of the new file.
        Returns:
        the new created file.
        Throws:
        java.lang.RuntimeException - if the path belongs to an existing non-empty directory.
        java.lang.RuntimeException - if the path belongs to an existing file.
        java.io.UncheckedIOException - if any I/O error is thrown when creating the new file.
      • newFolder

        public static java.io.File newFolder​(java.lang.String path)
        Creates a new directory using the given path.
        Parameters:
        path - the path of the new directory.
        Returns:
        the new created directory.
        Throws:
        java.lang.RuntimeException - if the path belongs to an existing non-empty directory.
        java.lang.RuntimeException - if the path belongs to an existing file.
        java.lang.RuntimeException - if any I/O error is thrown when creating the new directory.
      • createFileIfPathIsNotANonEmptyDirectory

        private static java.io.File createFileIfPathIsNotANonEmptyDirectory​(java.lang.String path)
      • cannotCreateNewFile

        private static java.io.UncheckedIOException cannotCreateNewFile​(java.lang.String path,
                                                                        java.lang.String reason)
      • cannotCreateNewFile

        private static java.io.UncheckedIOException cannotCreateNewFile​(java.lang.String path,
                                                                        java.lang.Exception cause)
      • cannotCreateNewFile

        private static java.io.UncheckedIOException cannotCreateNewFile​(java.lang.String path,
                                                                        java.lang.String reason,
                                                                        java.lang.Exception cause)
      • currentFolder

        public static java.io.File currentFolder()
        Returns the current directory.
        Returns:
        the current directory.
        Throws:
        java.io.UncheckedIOException - if the current directory cannot be obtained.
      • delete

        @Deprecated
        public static void delete​(java.io.File file)
        Deprecated.
        use https://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/FileUtils.html#forceDelete-java.io.File- instead
        Deletes the given file or directory.
        Parameters:
        file - the file or directory to delete.
      • contentOf

        public static java.lang.String contentOf​(java.io.File file,
                                                 java.lang.String charsetName)
        Loads the text content of a file into a character string.
        Parameters:
        file - the file.
        charsetName - the name of the character set to use.
        Returns:
        the content of the file.
        Throws:
        java.lang.IllegalArgumentException - if the given character set is not supported on this platform.
        java.io.UncheckedIOException - if an I/O exception occurs.
      • contentOf

        public static java.lang.String contentOf​(java.io.File file,
                                                 java.nio.charset.Charset charset)
        Loads the text content of a file into a character string.
        Parameters:
        file - the file.
        charset - the character set to use.
        Returns:
        the content of the file.
        Throws:
        java.lang.NullPointerException - if the given charset is null.
        java.io.UncheckedIOException - if an I/O exception occurs.
      • linesOf

        public static java.util.List<java.lang.String> linesOf​(java.io.File file,
                                                               java.nio.charset.Charset charset)
        Loads the text content of a file into a list of strings, each string corresponding to a line. The line endings are either \n, \r or \r\n.
        Parameters:
        file - the file.
        charset - the character set to use.
        Returns:
        the content of the file.
        Throws:
        java.lang.NullPointerException - if the given charset is null.
        java.io.UncheckedIOException - if an I/O exception occurs.
      • linesOf

        public static java.util.List<java.lang.String> linesOf​(java.io.File file,
                                                               java.lang.String charsetName)
        Loads the text content of a file into a list of strings, each string corresponding to a line. The line endings are either \n, \r or \r\n.
        Parameters:
        file - the file.
        charsetName - the name of the character set to use.
        Returns:
        the content of the file.
        Throws:
        java.lang.NullPointerException - if the given charset is null.
        java.io.UncheckedIOException - if an I/O exception occurs.
      • checkArgumentCharsetIsSupported

        private static void checkArgumentCharsetIsSupported​(java.lang.String charsetName)
      • getFileNameExtension

        public static java.util.Optional<java.lang.String> getFileNameExtension​(java.lang.String fileName)