java.nio.file.StandardCopyOption#REPLACE_EXISTING源码实例Demo

下面列出了java.nio.file.StandardCopyOption#REPLACE_EXISTING 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testCopyReplaceNonEmptyDirAllowed() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    FileSystemException exception = assertThrows(FileSystemException.class,
            () -> fileSystem.copy(createPath("/baz"), createPath("/foo"), options));
    assertEquals("/foo", exception.getFile());

    verify(getExceptionFactory()).createDeleteException(eq("/foo"), any(SftpException.class), eq(true));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
源代码2 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testCopyReplaceNonEmptyDirAllowedDifferentFileSystems() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    FileSystemException exception = assertThrows(FileSystemException.class,
            () -> fileSystem.copy(createPath("/baz"), createPath(fileSystem2, "/foo"), options));
    assertEquals("/foo", exception.getFile());

    verify(getExceptionFactory()).createDeleteException(eq("/foo"), any(SftpException.class), eq(true));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
源代码3 项目: localization_nifi   文件: FetchFile.java
protected void move(final File source, final File target, final boolean overwrite) throws IOException {
    final File targetDirectory = target.getParentFile();

    // convert to path and use Files.move instead of file.renameTo so that if we fail, we know why
    final Path targetPath = target.toPath();
    if (!targetDirectory.exists()) {
        Files.createDirectories(targetDirectory.toPath());
    }

    final CopyOption[] copyOptions = overwrite ? new CopyOption[] {StandardCopyOption.REPLACE_EXISTING} : new CopyOption[] {};
    Files.move(source.toPath(), targetPath, copyOptions);
}
 
源代码4 项目: openjdk-jdk9   文件: UnixCopyFile.java
static Flags fromCopyOptions(CopyOption... options) {
    Flags flags = new Flags();
    flags.followLinks = true;
    for (CopyOption option: options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            flags.followLinks = false;
            continue;
        }
        if (option == StandardCopyOption.COPY_ATTRIBUTES) {
            // copy all attributes but only fail if basic attributes
            // cannot be copied
            flags.copyBasicAttributes = true;
            flags.copyPosixAttributes = true;
            flags.copyNonPosixAttributes = true;
            flags.failIfUnableToCopyBasic = true;
            continue;
        }
        if (ExtendedOptions.INTERRUPTIBLE.matches(option)) {
            flags.interruptible = true;
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }
    return flags;
}
 
源代码5 项目: openjdk-jdk9   文件: UnixCopyFile.java
static Flags fromMoveOptions(CopyOption... options) {
    Flags flags = new Flags();
    for (CopyOption option: options) {
        if (option == StandardCopyOption.ATOMIC_MOVE) {
            flags.atomicMove = true;
            continue;
        }
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            // ignore
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }

    // a move requires that all attributes be copied but only fail if
    // the basic attributes cannot be copied
    flags.copyBasicAttributes = true;
    flags.copyPosixAttributes = true;
    flags.copyNonPosixAttributes = true;
    flags.failIfUnableToCopyBasic = true;
    return flags;
}
 
源代码6 项目: baratine   文件: JFileSystemProvider.java
@Override
public void copy(Path source, Path target, CopyOption... options)
  throws IOException
{
  JPath jSource = (JPath) source;
  JPath jTarget = (JPath) target;

  try (InputStream is = jSource.getBfsFile().openRead()) {
    if (is == null) {
      throw new IOException(L.l("unable to read from file {0}", source.toUri()));
    }

    ArrayList<WriteOption> bfsOptionList = new ArrayList<>();
    for (CopyOption option : options) {
      if (option == StandardCopyOption.REPLACE_EXISTING) {
        bfsOptionList.add(WriteOption.Standard.OVERWRITE);
      }
    }

    WriteOption []bfsOptions = new WriteOption[bfsOptionList.size()];
    bfsOptionList.toArray(bfsOptions);

    try (OutputStream os = jTarget.getBfsFile().openWrite(bfsOptions)) {
      if (os == null) {
        throw new IOException(L.l("unable to write to file {0}", target.toUri()));
      }

      IoUtil.copy(is, os);
    }
  }
}
 
源代码7 项目: sftp-fs   文件: CopyOptions.java
static CopyOptions forCopy(CopyOption... options) {

        boolean replaceExisting = false;

        for (CopyOption option : options) {
            if (option == StandardCopyOption.REPLACE_EXISTING) {
                replaceExisting = true;
            } else if (!isIgnoredCopyOption(option)) {
                throw Messages.fileSystemProvider().unsupportedCopyOption(option);
            }
        }

        return new CopyOptions(replaceExisting, Arrays.asList(options));
    }
 
源代码8 项目: sftp-fs   文件: CopyOptions.java
static CopyOptions forMove(boolean sameFileSystem, CopyOption... options) {
    boolean replaceExisting = false;

    for (CopyOption option : options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            replaceExisting = true;
        } else if (!(option == StandardCopyOption.ATOMIC_MOVE && sameFileSystem) && !isIgnoredCopyOption(option)) {
            throw Messages.fileSystemProvider().unsupportedCopyOption(option);
        }
    }

    return new CopyOptions(replaceExisting, Arrays.asList(options));
}
 
源代码9 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testCopyReplaceFileAllowed() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    fileSystem.copy(createPath("/baz"), createPath("/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
}
 
源代码10 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testCopyReplaceEmptyDirAllowed() throws IOException {
    addDirectory("/foo");
    addDirectory("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    fileSystem.copy(createPath("/baz"), createPath("/foo"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo")));
}
 
源代码11 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testCopyReplaceFileAllowedDifferentFileSystems() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    fileSystem.copy(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
}
 
源代码12 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testCopyReplaceEmptyDirAllowedDifferentFileSystems() throws IOException {
    addDirectory("/foo");
    addDirectory("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    fileSystem.copy(createPath("/baz"), createPath(fileSystem2, "/foo"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo")));
}
 
源代码13 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testMoveReplaceFileAllowed() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    fileSystem.move(createPath("/baz"), createPath("/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
源代码14 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testMoveReplaceEmptyDirAllowed() throws IOException {
    addDirectory("/foo");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    fileSystem.move(createPath("/baz"), createPath("/foo"), options);

    assertTrue(Files.isRegularFile(getPath("/foo")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
源代码15 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testMoveReplaceFileAllowedDifferentFileSystem() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
源代码16 项目: sftp-fs   文件: SFTPFileSystemTest.java
public void testMoveReplaceEmptyDirAllowedDifferentFileSystem() throws IOException {
    addDirectory("/foo");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo"), options);

    assertTrue(Files.isRegularFile(getPath("/foo")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
源代码17 项目: copybara   文件: CopyMoveVisitor.java
CopyMoveVisitor(Path before, Path after, @Nullable PathMatcher pathMatcher, boolean overwrite, boolean isCopy) {
  this.before = before;
  this.after = after;
  this.pathMatcher = pathMatcher;
  this.isCopy = isCopy;
  if (overwrite) {
    moveMode = new CopyOption[]{LinkOption.NOFOLLOW_LINKS, StandardCopyOption.REPLACE_EXISTING};
  } else {
    moveMode = new CopyOption[]{LinkOption.NOFOLLOW_LINKS};
  }
}
 
源代码18 项目: nifi   文件: FetchFile.java
protected void move(final File source, final File target, final boolean overwrite) throws IOException {
    final File targetDirectory = target.getParentFile();

    // convert to path and use Files.move instead of file.renameTo so that if we fail, we know why
    final Path targetPath = target.toPath();
    if (!targetDirectory.exists()) {
        Files.createDirectories(targetDirectory.toPath());
    }

    final CopyOption[] copyOptions = overwrite ? new CopyOption[] {StandardCopyOption.REPLACE_EXISTING} : new CopyOption[] {};
    Files.move(source.toPath(), targetPath, copyOptions);
}
 
源代码19 项目: PolyGlot   文件: IOHandler.java
public static void copyFile(Path fromLocation, Path toLocation, boolean replaceExisting) throws IOException {
    StandardCopyOption option = replaceExisting ? StandardCopyOption.REPLACE_EXISTING : StandardCopyOption.ATOMIC_MOVE;
    Files.copy(fromLocation, toLocation, option);
}
 
 同类方法