Class DeleteFileExtensions

java.lang.Object
io.github.astrapi69.file.delete.DeleteFileExtensions

public final class DeleteFileExtensions extends Object
The class DeleteFileExtensions helps delete files, leveraging new functionalities from java.nio.file introduced in JDK 17 for optimized handling
  • Method Details

    • checkFile

      public static Exception checkFile(File file)
      Checks if the File is a directory, exists, and is accessible
      Parameters:
      file - The File to check
      Returns:
      Null if all checks pass; otherwise, an appropriate Exception
    • delete

      public static void delete(Collection<File> files) throws IOException
      Tries to delete all given files in the collection. Caution: This can not be undone.
      Parameters:
      files - The files to delete
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • delete

      public static boolean delete(File file) throws IOException
      Tries to delete the specified file or directory recursively if it is a directory
      Parameters:
      file - The file or directory to delete
      Returns:
      true if deletion is successful; false otherwise
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • deleteAllFiles

      public static void deleteAllFiles(Path dirPath) throws IOException
      Deletes all files and directories recursively within the specified directory
      Parameters:
      dirPath - The path of the directory to delete files within
      Throws:
      IOException - if an I/O exception occurs
    • deleteAllFiles

      public static void deleteAllFiles(File file) throws IOException
      Deletes the File and if it is an directory it deletes his sub-directories recursively.
      Parameters:
      file - The File to delete.
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • deleteAllFilesWithSuffix

      public static void deleteAllFilesWithSuffix(File directory, String theSuffix) throws IOException
      Deletes all files with the given suffix recursively.
      Parameters:
      directory - The directory from where to delete the files with the given suffix.
      theSuffix - The suffix from the files to delete.
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • deleteFile

      public static void deleteFile(File fileToDelete) throws IOException
      Attempts to delete a specific file
      Parameters:
      fileToDelete - The file to delete
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • deleteFiles

      public static void deleteFiles(File file) throws IOException
      Tries to delete all files in the Directory.
      Parameters:
      file - The Directory to delete files.
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • deleteFilesWithFileFilter

      public static void deleteFilesWithFileFilter(File sourceDir, FileFilter includeFileFilter) throws FileIsNotADirectoryException, IOException, FileIsSecurityRestrictedException
      Tries to delete all files that match to the given includeFileFilter from the given source directory.
      Parameters:
      sourceDir - The source directory.
      includeFileFilter - The FileFilter for the files to be deleted. If null all files will be deleted.
      Throws:
      FileIsNotADirectoryException - Is thrown if the destination file is a directory.
      IOException - Is thrown if an error occurs by reading or writing.
      FileIsSecurityRestrictedException - Is thrown if the source file is security restricted.
    • deleteAllFilesWithPrefix

      public static void deleteAllFilesWithPrefix(File sourceDir, String prefix) throws FileIsNotADirectoryException, FileIsSecurityRestrictedException, IOException
      Tries to delete all files that match to the given prefix from the given source directory.
      Parameters:
      sourceDir - The source directory
      prefix - The prefix from the files to delete
      Throws:
      FileIsNotADirectoryException - Is thrown if the destination file is a directory.
      IOException - Is thrown if an error occurs by reading or writing.
      FileIsSecurityRestrictedException - Is thrown if the source file is security restricted.
    • deleteFilesWithFileFilter

      public static void deleteFilesWithFileFilter(File source, FileFilter includeFileFilter, FileFilter excludeFileFilter) throws FileIsNotADirectoryException, IOException, FileIsSecurityRestrictedException
      Tries to delete all files that match to the given includeFileFilter and does not delete the files that match the excludeFileFilter from the given source directory.
      Parameters:
      source - The source directory.
      includeFileFilter - The FileFilter for the files to be deleted. If null all files will be deleted.
      excludeFileFilter - The FileFilter for the files to be not deleted. If null no files will be excluded by delete process.
      Throws:
      FileIsNotADirectoryException - Is thrown if the destination file is a directory.
      IOException - Is thrown if an error occurs by reading or writing.
      FileIsSecurityRestrictedException - Is thrown if the source file is security restricted.
    • deleteFilesWithFilenameFilter

      public static void deleteFilesWithFilenameFilter(File source, FilenameFilter includeFilenameFilter) throws FileIsNotADirectoryException, IOException, FileIsSecurityRestrictedException
      Tries to delete all files that match to the given includeFilenameFilter from the given source directory.
      Parameters:
      source - The source directory.
      includeFilenameFilter - The FilenameFilter for the files to be deleted. If null all files will be deleted.
      Throws:
      FileIsNotADirectoryException - Is thrown if the destination file is a directory.
      IOException - Is thrown if an error occurs by reading or writing.
      FileIsSecurityRestrictedException - Is thrown if the source file is security restricted.
    • deleteFilesWithFilenameFilter

      public static void deleteFilesWithFilenameFilter(File source, FilenameFilter includeFilenameFilter, FilenameFilter excludeFilenameFilter) throws FileIsNotADirectoryException, IOException, FileIsSecurityRestrictedException
      Tries to delete all files that match to the given includeFilenameFilter and does not delete the files that match the excludeFilenameFilter from the given source directory.
      Parameters:
      source - The source directory.
      includeFilenameFilter - The FilenameFilter for the files to be deleted. If null all files will be deleted.
      excludeFilenameFilter - The FilenameFilter for the files to be not deleted. If null no files will be excluded by delete process.
      Throws:
      FileIsNotADirectoryException - Is thrown if the destination file is a directory.
      IOException - Is thrown if an error occurs by reading or writing.
      FileIsSecurityRestrictedException - Is thrown if the source file is security restricted.