Class FileOperationManager

java.lang.Object
com.helger.io.file.FileOperationManager
All Implemented Interfaces:
IFileOperationManager

@NotThreadSafe public class FileOperationManager extends Object implements IFileOperationManager
Manage file operations, and persist the last errors for later retrieval, as well as offering a callback mechanism for success/error handling.
Author:
Philip Helger
  • Field Details

  • Constructor Details

    • FileOperationManager

      public FileOperationManager()
      Default constructor without any registered callbacks.
  • Method Details

    • callbacks

      @ReturnsMutableObject public @NonNull com.helger.base.callback.CallbackList<IFileOperationCallback> callbacks()
      Get the mutable callback list for file operation events.
      Returns:
      The callback list. Never null.
    • getLastError

      public @Nullable FileIOError getLastError()
      Specified by:
      getLastError in interface IFileOperationManager
      Returns:
      The last error that occurred. May be null if no action was yet performed.
    • getLastOperation

      public @Nullable EFileIOOperation getLastOperation()
      Specified by:
      getLastOperation in interface IFileOperationManager
      Returns:
      The last operation that was executed, independent of error or success. May be null if no action was performed yet.
    • createDir

      public @NonNull FileIOError createDir(@NonNull File aDir)
      Create a new directory. The direct parent directory already needs to exist.
      Specified by:
      createDir in interface IFileOperationManager
      Parameters:
      aDir - The directory to be created. May not be null.
      Returns:
      A non-null error code.
    • createDirIfNotExisting

      public @NonNull FileIOError createDirIfNotExisting(@NonNull File aDir)
      Create a new directory if it does not exist. The direct parent directory already needs to exist.
      Specified by:
      createDirIfNotExisting in interface IFileOperationManager
      Parameters:
      aDir - The directory to be created if it does not exist. May not be null.
      Returns:
      A non-null error code.
    • createDirRecursive

      public @NonNull FileIOError createDirRecursive(@NonNull File aDir)
      Create a new directory. The parent directories are created if they are missing.
      Specified by:
      createDirRecursive in interface IFileOperationManager
      Parameters:
      aDir - The directory to be created. May not be null.
      Returns:
      A non-null error code.
    • createDirRecursiveIfNotExisting

      public @NonNull FileIOError createDirRecursiveIfNotExisting(@NonNull File aDir)
      Create a new directory if it does not exist. The direct parent directory already needs to exist.
      Specified by:
      createDirRecursiveIfNotExisting in interface IFileOperationManager
      Parameters:
      aDir - The directory to be created if it does not exist. May not be null.
      Returns:
      A non-null error code.
    • deleteDir

      public @NonNull FileIOError deleteDir(@NonNull File aDir)
      Delete an existing directory. The directory needs to be empty before it can be deleted.
      Specified by:
      deleteDir in interface IFileOperationManager
      Parameters:
      aDir - The directory to be deleted. May not be null.
      Returns:
      A non-null error code.
    • deleteDirIfExisting

      public @NonNull FileIOError deleteDirIfExisting(@NonNull File aDir)
      Delete an existing directory if it is existing. The directory needs to be empty before it can be deleted.
      Specified by:
      deleteDirIfExisting in interface IFileOperationManager
      Parameters:
      aDir - The directory to be deleted. May not be null.
      Returns:
      A non-null error code.
    • deleteDirRecursive

      public @NonNull FileIOError deleteDirRecursive(@NonNull File aDir)
      Delete an existing directory including all child objects.
      Specified by:
      deleteDirRecursive in interface IFileOperationManager
      Parameters:
      aDir - The directory to be deleted. May not be null.
      Returns:
      A non-null error code.
    • deleteDirRecursiveIfExisting

      public @NonNull FileIOError deleteDirRecursiveIfExisting(@NonNull File aDir)
      Delete an existing directory including all child objects if it is existing.
      Specified by:
      deleteDirRecursiveIfExisting in interface IFileOperationManager
      Parameters:
      aDir - The directory to be deleted. May not be null.
      Returns:
      A non-null error code.
    • deleteFile

      public @NonNull FileIOError deleteFile(@NonNull File aFile)
      Delete an existing file.
      Specified by:
      deleteFile in interface IFileOperationManager
      Parameters:
      aFile - The file to be deleted. May not be null.
      Returns:
      A non-null error code.
    • deleteFileIfExisting

      public @NonNull FileIOError deleteFileIfExisting(@NonNull File aFile)
      Delete a file if it is existing.
      Specified by:
      deleteFileIfExisting in interface IFileOperationManager
      Parameters:
      aFile - The file to be deleted. May not be null.
      Returns:
      A non-null error code.
    • renameDir

      public @NonNull FileIOError renameDir(@NonNull File aSourceDir, @NonNull File aTargetDir)
      Rename a directory.
      Specified by:
      renameDir in interface IFileOperationManager
      Parameters:
      aSourceDir - The original directory name. May not be null.
      aTargetDir - The destination directory name. May not be null.
      Returns:
      A non-null error code.
    • renameFile

      public @NonNull FileIOError renameFile(@NonNull File aSourceFile, @NonNull File aTargetFile)
      Rename a file.
      Specified by:
      renameFile in interface IFileOperationManager
      Parameters:
      aSourceFile - The original file name. May not be null.
      aTargetFile - The destination file name. May not be null.
      Returns:
      A non-null error code.
    • copyDirRecursive

      public @NonNull FileIOError copyDirRecursive(@NonNull File aSourceDir, @NonNull File aTargetDir)
      Copy a directory including all child objects.
      Specified by:
      copyDirRecursive in interface IFileOperationManager
      Parameters:
      aSourceDir - The source directory to be copied. May not be null.
      aTargetDir - The destination directory where to be copied. This directory may not be existing. May not be null.
      Returns:
      A non-null error code.
    • copyFile

      public @NonNull FileIOError copyFile(@NonNull File aSourceFile, @NonNull File aTargetFile)
      Copies the source file to the target file.
      Specified by:
      copyFile in interface IFileOperationManager
      Parameters:
      aSourceFile - The source file to use. May not be null. Needs to be an existing file.
      aTargetFile - The destination files. May not be null and may not be an existing file.
      Returns:
      A non-null error code.
    • toString

      public String toString()
      Overrides:
      toString in class Object