/** * Create a new directory if it does not exist. The direct parent directory already needs to * exist. * * @param aDir The directory to be created if it does not exist. May not be <code>null</code>. * @return A non-<code>null</code> error code. */ @Nonnull public static FileIOError createDirIfNotExisting(@Nonnull final File aDir) { final FileIOError aError = createDir(aDir); if (aError.getErrorCode().equals(EFileIOErrorCode.TARGET_ALREADY_EXISTS)) return EFileIOErrorCode.NO_ERROR.getAsIOError(EFileIOOperation.CREATE_DIR, aDir); return aError; }
/** * Delete a file if it is existing. * * @param aFile The file to be deleted. May not be <code>null</code>. * @return A non-<code>null</code> error code. */ @Nonnull public static FileIOError deleteFileIfExisting(@Nonnull final File aFile) { final FileIOError aError = deleteFile(aFile); if (aError.getErrorCode().equals(EFileIOErrorCode.SOURCE_DOES_NOT_EXIST)) return EFileIOErrorCode.NO_ERROR.getAsIOError(EFileIOOperation.DELETE_FILE, aFile); return aError; }
/** * Delete an existing directory including all child objects if it is existing. * * @param aDir The directory to be deleted. May not be <code>null</code>. * @return A non-<code>null</code> error code. */ @Nonnull public static FileIOError deleteDirRecursiveIfExisting(@Nonnull final File aDir) { final FileIOError aError = deleteDirRecursive(aDir); if (aError.getErrorCode().equals(EFileIOErrorCode.SOURCE_DOES_NOT_EXIST)) return EFileIOErrorCode.NO_ERROR.getAsIOError(EFileIOOperation.DELETE_DIR_RECURSIVE, aDir); return aError; }
/** * Copy a directory including all child objects. * * @param aSourceDir The source directory to be copied. May not be <code>null</code>. * @param aTargetDir The destination directory where to be copied. This directory may not be * existing. May not be <code>null</code>. * @return A non-<code>null</code> error code. */ @Nonnull public static FileIOError copyDirRecursive( @Nonnull final File aSourceDir, @Nonnull final File aTargetDir) { ValueEnforcer.notNull(aSourceDir, "SourceDirectory"); ValueEnforcer.notNull(aTargetDir, "TargetDirectory"); // Does the source directory exist? if (!FileUtils.existsDir(aSourceDir)) return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError( EFileIOOperation.COPY_DIR_RECURSIVE, aSourceDir); // Are source and target different? if (EqualsUtils.equals(aSourceDir, aTargetDir)) return EFileIOErrorCode.SOURCE_EQUALS_TARGET.getAsIOError( EFileIOOperation.COPY_DIR_RECURSIVE, aSourceDir); // Is the source a parent of target? if (FileUtils.isParentDirectory(aSourceDir, aTargetDir)) return EFileIOErrorCode.TARGET_IS_CHILD_OF_SOURCE.getAsIOError( EFileIOOperation.COPY_DIR_RECURSIVE, aSourceDir, aTargetDir); // Does the target directory already exist? if (aTargetDir.exists()) return EFileIOErrorCode.TARGET_ALREADY_EXISTS.getAsIOError( EFileIOOperation.COPY_DIR_RECURSIVE, aTargetDir); // Is the source directory readable? if (!FileUtils.canRead(aSourceDir)) return EFileIOErrorCode.SOURCE_NOT_READABLE.getAsIOError( EFileIOOperation.COPY_DIR_RECURSIVE, aSourceDir); // Is the target parent directory writable? final File aTargetParentDir = aTargetDir.getParentFile(); if (aTargetParentDir != null && aTargetParentDir.exists() && !FileUtils.canWrite(aTargetParentDir)) return EFileIOErrorCode.TARGET_PARENT_NOT_WRITABLE.getAsIOError( EFileIOOperation.COPY_DIR_RECURSIVE, aTargetDir); FileIOError eCode; // Ensure the targets parent directory is present eCode = createDirRecursive(aTargetDir); if (eCode.isFailure()) return eCode; for (final File aChild : FileUtils.getDirectoryContent(aSourceDir)) { if (aChild.isDirectory()) { // Skip "." and ".." if (FilenameHelper.isSystemInternalDirectory(aChild.getName())) continue; // Copy directory eCode = copyDirRecursive(aChild, new File(aTargetDir, aChild.getName())); if (eCode.isFailure()) return eCode; } else if (aChild.isFile()) { // Copy a file eCode = copyFile(aChild, new File(aTargetDir, aChild.getName())); if (eCode.isFailure()) return eCode; } else { // Neither directory not file - don't know how to handle return EFileIOErrorCode.OBJECT_CANNOT_BE_HANDLED.getAsIOError( EFileIOOperation.COPY_DIR_RECURSIVE, aChild); } } // Done return EFileIOErrorCode.NO_ERROR.getAsIOError( EFileIOOperation.COPY_DIR_RECURSIVE, aSourceDir, aTargetDir); }