Exemplo n.º 1
0
  /**
   * Moves a file from one directory to another, by a rename op.
   *
   * @param fs
   * @param fileName The base filename of the file to move, not the complete path
   * @param fromDirectory The directory currently containing filename
   * @param toDirectory The directory to host filename - must be in the same disk partition as
   *     filename
   * @param allowOverwriteTarget
   * @throws java.io.IOException
   */
  static void moveFile(
      FileSystemAbstraction fs,
      String fileName,
      File fromDirectory,
      File toDirectory,
      boolean allowSkipNonExistentFiles,
      boolean allowOverwriteTarget)
      throws IOException {
    File sourceFile = new File(fromDirectory, fileName);
    if (allowSkipNonExistentFiles
        && !fs.fileExists(
            sourceFile)) { // The source file doesn't exist and we allow skipping, so return
      return;
    }

    File toFile = new File(toDirectory, fileName);
    if (allowOverwriteTarget && fs.fileExists(toFile)) {
      fs.deleteFile(toFile);
    }

    fs.moveToDirectory(sourceFile, toDirectory);
  }