src/share/sample/nio/file/Copy.java
Print this page
*** 50,60 ****
return (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"));
}
/**
* Copy source file to target location. If {@code prompt} is true then
! * prompted user to overwrite target if it exists. The {@code preserve}
* parameter determines if file attributes should be copied/preserved.
*/
static void copyFile(Path source, Path target, boolean prompt, boolean preserve) {
CopyOption[] options = (preserve) ?
new CopyOption[] { COPY_ATTRIBUTES, REPLACE_EXISTING } :
--- 50,60 ----
return (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"));
}
/**
* Copy source file to target location. If {@code prompt} is true then
! * prompt user to overwrite target if it exists. The {@code preserve}
* parameter determines if file attributes should be copied/preserved.
*/
static void copyFile(Path source, Path target, boolean prompt, boolean preserve) {
CopyOption[] options = (preserve) ?
new CopyOption[] { COPY_ATTRIBUTES, REPLACE_EXISTING } :
*** 61,71 ****
new CopyOption[] { REPLACE_EXISTING };
if (!prompt || target.notExists() || okayToOverwrite(target)) {
try {
source.copyTo(target, options);
} catch (IOException x) {
! System.err.format("Unable to create: %s: %s%n", target, x);
}
}
}
/**
--- 61,71 ----
new CopyOption[] { REPLACE_EXISTING };
if (!prompt || target.notExists() || okayToOverwrite(target)) {
try {
source.copyTo(target, options);
} catch (IOException x) {
! System.err.format("Unable to copy: %s: %s%n", source, x);
}
}
}
/**
*** 122,138 ****
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
// fix up modification time of directory when done
if (exc == null && preserve) {
try {
BasicFileAttributes attrs = Attributes.readBasicFileAttributes(dir);
- Path newdir = target.resolve(source.relativize(dir));
Attributes.setLastModifiedTime(newdir,
attrs.lastModifiedTime(), attrs.resolution());
} catch (IOException x) {
! // ignore
}
}
return CONTINUE;
}
--- 122,138 ----
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
// fix up modification time of directory when done
if (exc == null && preserve) {
+ Path newdir = target.resolve(source.relativize(dir));
try {
BasicFileAttributes attrs = Attributes.readBasicFileAttributes(dir);
Attributes.setLastModifiedTime(newdir,
attrs.lastModifiedTime(), attrs.resolution());
} catch (IOException x) {
! System.err.format("Unable to copy all attributes to: %s: %s%n", newdir, x);
}
}
return CONTINUE;
}
*** 189,198 ****
--- 189,199 ----
// check if target is a directory
boolean isDir = false;
try {
isDir = Attributes.readBasicFileAttributes(target).isDirectory();
} catch (IOException x) {
+ // ignore (probably target does not exist)
}
// copy each source file/directory to target
for (i=0; i<source.length; i++) {
Path dest = (isDir) ? target.resolve(source[i].getName()) : target;
*** 199,217 ****
if (recursive) {
// follow links when copying files
EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
TreeCopier tc = new TreeCopier(source[i], dest, prompt, preserve);
! Files.walkFileTree(source[i], opts, -1, tc);
} else {
// not recursive so source must not be a directory
try {
if (Attributes.readBasicFileAttributes(source[i]).isDirectory()) {
System.err.format("%s: is a directory%n", source[i]);
continue;
}
! } catch (IOException x) { }
copyFile(source[i], dest, prompt, preserve);
}
}
}
}
--- 200,220 ----
if (recursive) {
// follow links when copying files
EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
TreeCopier tc = new TreeCopier(source[i], dest, prompt, preserve);
! Files.walkFileTree(source[i], opts, Integer.MAX_VALUE, tc);
} else {
// not recursive so source must not be a directory
try {
if (Attributes.readBasicFileAttributes(source[i]).isDirectory()) {
System.err.format("%s: is a directory%n", source[i]);
continue;
}
! } catch (IOException x) {
! // assume not directory
! }
copyFile(source[i], dest, prompt, preserve);
}
}
}
}