Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Using nio Files.copy to Move Directory

Tags:

java

file

nio

I am new to the nio class, and am having trouble moving a directory of files to a newly created directory.

I first create 2 directories with:

File sourceDir = new File(sourceDirStr); //this directory already exists
File destDir = new File(destDirectoryStr); //this is a new directory

I then try to copy the existing files into the new directory, using:

Path destPath = destDir.toPath();
for (int i = 0; i < sourceSize; i++) {
    Path sourcePath = sourceDir.listFiles()[i].toPath();
    Files.copy(sourcePath, destPath.resolve(sourcePath.getFileName()));
}

This throws the following error:

Exception in thread "main" java.nio.file.FileSystemException: destDir/Experiment.log: Not a directory

I know that destDir/Experiment.log is not an existing directory; it should be a new file as a result of the Files.copy operation. Could someone point out where my operation is going wrong? Thanks!

like image 814
Adam_G Avatar asked Feb 28 '13 14:02

Adam_G


1 Answers

This is my solution for recursively moving a directory from source to target. It works like a charm.

public static void move(Path source, Path target) throws IOException {

    class FileMover extends SimpleFileVisitor<Path> {
        private Path source;
        private Path target;

        private FileMover(Path source, Path target) {
            this.source = source;
            this.target = target;
        }

        @Override
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            Files.move(file, target.resolve(source.relativize(file)),
                StandardCopyOption.REPLACE_EXISTING);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
            Path newDir = target.resolve(source.relativize(dir));
            try {
                Files.copy(dir, newDir,
                    StandardCopyOption.COPY_ATTRIBUTES,
                    StandardCopyOption.REPLACE_EXISTING);
            } catch (DirectoryNotEmptyException e) {
                // ignore and skip
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
            Path newDir = target.resolve(source.relativize(dir));
            FileTime time = Files.getLastModifiedTime(dir);
            Files.setLastModifiedTime(newDir, time);
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }
    }

    FileMover fm = new FileMover(source, target);
    EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);

    Files.walkFileTree(source, opts, Integer.MAX_VALUE, fm);
}
like image 186
Downhillski Avatar answered Oct 01 '22 23:10

Downhillski