src/share/sample/nio/file/Copy.java

Print this page




  28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30  */
  31 
  32 import java.nio.file.*;
  33 import static java.nio.file.StandardCopyOption.*;
  34 import java.nio.file.attribute.*;
  35 import static java.nio.file.FileVisitResult.*;
  36 import java.io.IOException;
  37 import java.util.*;
  38 
  39 /**
  40  * Sample code that copies files in a similar manner to the cp(1) program.
  41  */
  42 
  43 public class Copy {
  44 
  45     /**
  46      * Returns {@code true} if okay to overwrite a  file ("cp -i")
  47      */
  48     static boolean okayToOverwrite(FileRef file) {
  49         String answer = System.console().readLine("overwrite %s (yes/no)? ", file);
  50         return (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"));
  51     }
  52 
  53     /**
  54      * Copy source file to target location. If {@code prompt} is true then
  55      * prompt user to overwrite target if it exists. The {@code preserve}
  56      * parameter determines if file attributes should be copied/preserved.
  57      */
  58     static void copyFile(Path source, Path target, boolean prompt, boolean preserve) {
  59         CopyOption[] options = (preserve) ?
  60             new CopyOption[] { COPY_ATTRIBUTES, REPLACE_EXISTING } :
  61             new CopyOption[] { REPLACE_EXISTING };
  62         if (!prompt || target.notExists() || okayToOverwrite(target)) {
  63             try {
  64                 source.copyTo(target, options);
  65             } catch (IOException x) {
  66                 System.err.format("Unable to copy: %s: %s%n", source, x);
  67             }
  68         }
  69     }
  70 
  71     /**
  72      * A {@code FileVisitor} that copies a file-tree ("cp -r")
  73      */
  74     static class TreeCopier implements FileVisitor<Path> {
  75         private final Path source;
  76         private final Path target;
  77         private final boolean prompt;
  78         private final boolean preserve;
  79 
  80         TreeCopier(Path source, Path target, boolean prompt, boolean preserve) {
  81             this.source = source;
  82             this.target = target;
  83             this.prompt = prompt;
  84             this.preserve = preserve;
  85         }
  86 
  87         @Override
  88         public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
  89             // before visiting entries in a directory we copy the directory
  90             // (okay if directory already exists).
  91             CopyOption[] options = (preserve) ?
  92                 new CopyOption[] { COPY_ATTRIBUTES } : new CopyOption[0];
  93 
  94             Path newdir = target.resolve(source.relativize(dir));
  95             try {
  96                 dir.copyTo(newdir, options);
  97             } catch (FileAlreadyExistsException x) {
  98                 // ignore
  99             } catch (IOException x) {
 100                 System.err.format("Unable to create: %s: %s%n", newdir, x);
 101                 return SKIP_SUBTREE;
 102             }
 103             return CONTINUE;
 104         }
 105 
 106         @Override
 107         public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
 108             copyFile(file, target.resolve(source.relativize(file)),
 109                      prompt, preserve);
 110             return CONTINUE;
 111         }
 112 
 113         @Override
 114         public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
 115             // fix up modification time of directory when done
 116             if (exc == null && preserve) {
 117                 Path newdir = target.resolve(source.relativize(dir));
 118                 try {
 119                     BasicFileAttributes attrs = Attributes.readBasicFileAttributes(dir);
 120                     Attributes.setLastModifiedTime(newdir, attrs.lastModifiedTime());
 121                 } catch (IOException x) {
 122                     System.err.format("Unable to copy all attributes to: %s: %s%n", newdir, x);
 123                 }
 124             }
 125             return CONTINUE;
 126         }
 127 
 128         @Override
 129         public FileVisitResult visitFileFailed(Path file, IOException exc) {
 130             if (exc instanceof FileSystemLoopException) {
 131                 System.err.println("cycle detected: " + file);
 132             } else {
 133                 System.err.format("Unable to copy: %s: %s%n", file, exc);
 134             }
 135             return CONTINUE;
 136         }
 137     }
 138 
 139     static void usage() {
 140         System.err.println("java Copy [-ip] source... target");


 163                     case 'p' : preserve = true; break;
 164                     default : usage();
 165                 }
 166             }
 167             argi++;
 168         }
 169 
 170         // remaining arguments are the source files(s) and the target location
 171         int remaining = args.length - argi;
 172         if (remaining < 2)
 173             usage();
 174         Path[] source = new Path[remaining-1];
 175         int i=0;
 176         while (remaining > 1) {
 177             source[i++] = Paths.get(args[argi++]);
 178             remaining--;
 179         }
 180         Path target = Paths.get(args[argi]);
 181 
 182         // check if target is a directory
 183         boolean isDir = false;
 184         try {
 185             isDir = Attributes.readBasicFileAttributes(target).isDirectory();
 186         } catch (IOException x) {
 187             // ignore (probably target does not exist)
 188         }
 189 
 190         // copy each source file/directory to target
 191         for (i=0; i<source.length; i++) {
 192             Path dest = (isDir) ? target.resolve(source[i].getName()) : target;
 193 
 194             if (recursive) {
 195                 // follow links when copying files
 196                 EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
 197                 TreeCopier tc = new TreeCopier(source[i], dest, prompt, preserve);
 198                 Files.walkFileTree(source[i], opts, Integer.MAX_VALUE, tc);
 199             } else {
 200                 // not recursive so source must not be a directory
 201                 try {
 202                     if (Attributes.readBasicFileAttributes(source[i]).isDirectory()) {
 203                         System.err.format("%s: is a directory%n", source[i]);
 204                         continue;
 205                     }
 206                 } catch (IOException x) {
 207                     // assume not directory
 208                 }
 209                 copyFile(source[i], dest, prompt, preserve);
 210             }
 211         }
 212     }
 213 }


  28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30  */
  31 
  32 import java.nio.file.*;
  33 import static java.nio.file.StandardCopyOption.*;
  34 import java.nio.file.attribute.*;
  35 import static java.nio.file.FileVisitResult.*;
  36 import java.io.IOException;
  37 import java.util.*;
  38 
  39 /**
  40  * Sample code that copies files in a similar manner to the cp(1) program.
  41  */
  42 
  43 public class Copy {
  44 
  45     /**
  46      * Returns {@code true} if okay to overwrite a  file ("cp -i")
  47      */
  48     static boolean okayToOverwrite(Path file) {
  49         String answer = System.console().readLine("overwrite %s (yes/no)? ", file);
  50         return (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"));
  51     }
  52 
  53     /**
  54      * Copy source file to target location. If {@code prompt} is true then
  55      * prompt user to overwrite target if it exists. The {@code preserve}
  56      * parameter determines if file attributes should be copied/preserved.
  57      */
  58     static void copyFile(Path source, Path target, boolean prompt, boolean preserve) {
  59         CopyOption[] options = (preserve) ?
  60             new CopyOption[] { COPY_ATTRIBUTES, REPLACE_EXISTING } :
  61             new CopyOption[] { REPLACE_EXISTING };
  62         if (!prompt || Files.notExists(target) || okayToOverwrite(target)) {
  63             try {
  64                 Files.copy(source, target, options);
  65             } catch (IOException x) {
  66                 System.err.format("Unable to copy: %s: %s%n", source, x);
  67             }
  68         }
  69     }
  70 
  71     /**
  72      * A {@code FileVisitor} that copies a file-tree ("cp -r")
  73      */
  74     static class TreeCopier implements FileVisitor<Path> {
  75         private final Path source;
  76         private final Path target;
  77         private final boolean prompt;
  78         private final boolean preserve;
  79 
  80         TreeCopier(Path source, Path target, boolean prompt, boolean preserve) {
  81             this.source = source;
  82             this.target = target;
  83             this.prompt = prompt;
  84             this.preserve = preserve;
  85         }
  86 
  87         @Override
  88         public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
  89             // before visiting entries in a directory we copy the directory
  90             // (okay if directory already exists).
  91             CopyOption[] options = (preserve) ?
  92                 new CopyOption[] { COPY_ATTRIBUTES } : new CopyOption[0];
  93 
  94             Path newdir = target.resolve(source.relativize(dir));
  95             try {
  96                 Files.copy(dir, newdir, options);
  97             } catch (FileAlreadyExistsException x) {
  98                 // ignore
  99             } catch (IOException x) {
 100                 System.err.format("Unable to create: %s: %s%n", newdir, x);
 101                 return SKIP_SUBTREE;
 102             }
 103             return CONTINUE;
 104         }
 105 
 106         @Override
 107         public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
 108             copyFile(file, target.resolve(source.relativize(file)),
 109                      prompt, preserve);
 110             return CONTINUE;
 111         }
 112 
 113         @Override
 114         public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
 115             // fix up modification time of directory when done
 116             if (exc == null && preserve) {
 117                 Path newdir = target.resolve(source.relativize(dir));
 118                 try {
 119                     FileTime time = Files.getLastModifiedTime(dir);
 120                     Files.setLastModifiedTime(newdir, time);
 121                 } catch (IOException x) {
 122                     System.err.format("Unable to copy all attributes to: %s: %s%n", newdir, x);
 123                 }
 124             }
 125             return CONTINUE;
 126         }
 127 
 128         @Override
 129         public FileVisitResult visitFileFailed(Path file, IOException exc) {
 130             if (exc instanceof FileSystemLoopException) {
 131                 System.err.println("cycle detected: " + file);
 132             } else {
 133                 System.err.format("Unable to copy: %s: %s%n", file, exc);
 134             }
 135             return CONTINUE;
 136         }
 137     }
 138 
 139     static void usage() {
 140         System.err.println("java Copy [-ip] source... target");


 163                     case 'p' : preserve = true; break;
 164                     default : usage();
 165                 }
 166             }
 167             argi++;
 168         }
 169 
 170         // remaining arguments are the source files(s) and the target location
 171         int remaining = args.length - argi;
 172         if (remaining < 2)
 173             usage();
 174         Path[] source = new Path[remaining-1];
 175         int i=0;
 176         while (remaining > 1) {
 177             source[i++] = Paths.get(args[argi++]);
 178             remaining--;
 179         }
 180         Path target = Paths.get(args[argi]);
 181 
 182         // check if target is a directory
 183         boolean isDir = Files.isDirectory(target);





 184 
 185         // copy each source file/directory to target
 186         for (i=0; i<source.length; i++) {
 187             Path dest = (isDir) ? target.resolve(source[i].getFileName()) : target;
 188 
 189             if (recursive) {
 190                 // follow links when copying files
 191                 EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
 192                 TreeCopier tc = new TreeCopier(source[i], dest, prompt, preserve);
 193                 Files.walkFileTree(source[i], opts, Integer.MAX_VALUE, tc);
 194             } else {
 195                 // not recursive so source must not be a directory
 196                 if (Files.isDirectory(source[i])) {

 197                     System.err.format("%s: is a directory%n", source[i]);
 198                     continue;
 199                 }



 200                 copyFile(source[i], dest, prompt, preserve);
 201             }
 202         }
 203     }
 204 }