Class FileUtilities

    • Method Detail

      • canWrite

        public static boolean canWrite​(Path path)
                                throws NoSuchFileException
        Checks whether a file can be written or not.
        Parameters:
        path - path
        Returns:
        true, if we can write
        Throws:
        NoSuchFileException
      • notExists

        public static boolean notExists​(Path file,
                                        long maxAgeInMS)
        Tests whether the given file does not exist. Results of the test are cached to avoid accessing the file system or network too often.
        Parameters:
        file - file
        maxAgeInMS - max age of the returned result in milliseconds
        Returns:
        true if we know for sure that the file didn't exist up to maxAgeInMS ago
        See Also:
        Files.notExists(Path, LinkOption...), DirectoryFileExistence
      • getExtension

        public static String getExtension​(Path file)
        Get extension (with the dot).
        Parameters:
        file - file
        Returns:
        extension (with '.') or empty string.
      • getExtensionWithoutDot

        public static String getExtensionWithoutDot​(Path file)
        Get extension (without the dot).
        Parameters:
        file - file
        Returns:
        extension (without '.') or empty string.
      • getExtension

        public static String getExtension​(String filename)
        Get extension (with the dot).
        Parameters:
        filename - filename
        Returns:
        extension (with '.') or empty string.
      • getExtensionWithoutDot

        public static String getExtensionWithoutDot​(String filename)
        Get extension (without the dot).
        Parameters:
        filename - filename
        Returns:
        extension (without '.') or empty string.
      • removeExtension

        public static String removeExtension​(String filename)
        Remove file extension, i.e. everything after the last dot, including the dot.
        Parameters:
        filename - filename
        Returns:
        filename without extension
      • injectBeforeExtension

        public static String injectBeforeExtension​(String filename,
                                                   String injection)
        Inject an arbitrary string before the extension. Useful for transforming image filename like image.png to [email protected]
        Parameters:
        filename - filename
        injection - injection, arbitrary string
        Returns:
        modified file name
      • createValidFilename

        public static String createValidFilename​(String name)
        Creates a name that only contains letters, digits, spaces and underscores.
        Parameters:
        name - name
        Returns:
        valid name
      • copy

        public static void copy​(InputStream in,
                                Path to)
                         throws IOException

        Copy a stream to a file, replacing the file if it exists. Resource management of the inputstream is left to the caller.

        Equivalent with: Files.copy(in, to, StandardCopyOption.REPLACE_EXISTING)

        Parameters:
        in - input stream
        to - file to write to
        Throws:
        IOException - if something goes wrong
      • copy

        public static void copy​(Path from,
                                Path to)
                         throws IOException
        Copy a file, preserving its attributes, replacing the target file, if it exists.

        Equivalent with: Files.copy(from, to, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING)

        Parameters:
        from - file to copy
        to - file to copy to
        Throws:
        IOException - if something goes wrong
      • copyFiles

        public static void copyFiles​(Path fromDir,
                                     Path toDir)
                              throws IOException
        Copy all regular files (i.e. no directories) from one directory to another.
        Parameters:
        fromDir - from dir
        toDir - to dir
        Throws:
        IOException - exception
      • isVolumeAvailable

        public static boolean isVolumeAvailable​(Path file)
        Indicates whether a drive or volume that a file is in is mounted.
        Parameters:
        file - file to check
        Returns:
        true or false
      • getVolume

        public static Path getVolume​(Path file)
        Extracts the volume/drive part from a file.
        Parameters:
        file - file
        Returns:
        volume or drive or null, if none could be discovered
      • getVolumeName

        public static String getVolumeName​(Path file)
        Volume name.
        Parameters:
        file - file
        Returns:
        volume or null, if none available
      • deleteRecursively

        public static void deleteRecursively​(Path path)
                                      throws IOException
        Deletes a directory recursively, thus allowing its deletion.
        Parameters:
        path - path
        Throws:
        IOException - if deletion fails
      • delete

        public static void delete​(Path... files)
        Delete the files in the given array.
        Parameters:
        files - files
      • copy

        public static void copy​(Path[] orig,
                                Path[] copies)
                         throws IOException
        Copy an array of files to a new array of files. If a copy file name is empty, a temp file with the same extension as the original is created automatically.
        Parameters:
        orig - originals
        copies - copies
        Throws:
        IOException - if something goes wrong
      • getFreeFile

        public static Path getFreeFile​(Path file)
                                throws IOException
        Attempt to find a free file name, based on a given filename, while preserving the extension.
        Parameters:
        file - file
        Returns:
        free file
        Throws:
        IOException - if we fail.