Class FileUtils


  • public final class FileUtils
    extends Object
    This class provides some helper methods to deal with files.
    • Constructor Detail

      • FileUtils

        public FileUtils()
    • Method Detail

      • getFile

        public static File getFile​(String first,
                                   String... more)
        Get a file from the given path elements.
        Parameters:
        first - the first path element
        more - the additional path elements
        Returns:
        the file
      • getFile

        public static File getFile​(File directory,
                                   String next,
                                   String... more)
        Get a file from the given path elements.
        Parameters:
        directory - the parent directory
        next - next path element
        more - additional path elements
        Returns:
        the file
      • copyDirectory

        public static void copyDirectory​(File source,
                                         File target)
                                  throws IOException
        Copy the source directory to the target directory.
        Parameters:
        source - the source directory
        target - the target directory
        Throws:
        IOException - if IO error occurs during copying
      • copyDirectory

        public static void copyDirectory​(Path source,
                                         Path target)
                                  throws IOException
        Copy the source directory to the target directory.
        Parameters:
        source - the source directory
        target - the target directory
        Throws:
        IOException - if IO error occurs during copying
      • deleteDirectory

        public static void deleteDirectory​(File directory)
                                    throws IOException
        Delete the given directory recursively.
        Parameters:
        directory - the directory to delete
        Throws:
        IOException - in case deletion is unsuccessful
      • deleteDirectory

        public static void deleteDirectory​(Path directory)
                                    throws IOException
        Delete the given directory recursively.
        Parameters:
        directory - the directory to delete
        Throws:
        IOException - in case deletion is unsuccessful
      • safeFileName

        public static String safeFileName​(String originalFileName)
        Sanitizes a filename, replacing with dash
        • Removes special characters that are illegal in filenames on certain operating systems
        • Replaces spaces and consecutive underscore with a single dash
        • Trims dot, dash and underscore from beginning and end of filename (with and without the extension part)
          Parameters:
          originalFileName - The filename to be sanitized
          Returns:
          string The sanitized filename
        • getExtension

          public static String getExtension​(String fileName)
          Gets the extension part of the given fileName

          It returns the extension of the fileName after the last dot

          Parameters:
          fileName - the fileName to retrieve the extension of
          Returns:
          the extension of the file or an empty string is not found or null if the given filename is null
        • stripExtension

          public static String stripExtension​(String fileName)
          Strip the extension part of the given fileName

          It returns the name of file before the last dot

          Parameters:
          fileName - the fileName to strip the extension
          Returns:
          the file name