下面列出了java.nio.file.StandardCopyOption#REPLACE_EXISTING 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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")));
}
@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")));
}
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);
}
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;
}
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;
}
@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);
}
}
}
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));
}
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));
}
@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")));
}
@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")));
}
@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")));
}
@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")));
}
@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")));
}
@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")));
}
@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")));
}
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")));
}
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};
}
}
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);
}
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);
}