11

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!

Adam_G
  • 7,337
  • 20
  • 86
  • 148
  • 2
    Does `destDir` exist on the disk though? If not you might have to create it using [`File#mkdirs()`](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdirs()) first. – millimoose Feb 28 '13 at 14:23
  • I've run `destDir.exists()`, which returns `True`. It almost sounds like it thinks `destDir/Experiment.log` should be a directory. Is that not the case, though? – Adam_G Feb 28 '13 at 16:59

4 Answers4

15

You need to use walkFileTree to copy directories. If you use Files.copy on a directory only an empty directory will be created.

Following code taken/adapted from http://codingjunkie.net/java-7-copy-move/

File src = new File("c:\\temp\\srctest");
File dest = new File("c:\\temp\\desttest");
Path srcPath = src.toPath();
Path destPath = dest.toPath();

Files.walkFileTree(srcPath, new CopyDirVisitor(srcPath, destPath, StandardCopyOption.REPLACE_EXISTING));

public static class CopyDirVisitor extends SimpleFileVisitor<Path>
{
    private final Path fromPath;
    private final Path toPath;
    private final CopyOption copyOption;

    public CopyDirVisitor(Path fromPath, Path toPath, CopyOption copyOption)
    {
        this.fromPath = fromPath;
        this.toPath = toPath;
        this.copyOption = copyOption;
    }

    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException
    {
        Path targetPath = toPath.resolve(fromPath.relativize(dir));
        if( !Files.exists(targetPath) )
        {
            Files.createDirectory(targetPath);
        }
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
    {
        Files.copy(file, toPath.resolve(fromPath.relativize(file)), copyOption);
        return FileVisitResult.CONTINUE;
    }
}
AaronHolland
  • 1,595
  • 1
  • 16
  • 32
4

Simply make the destination directory if it doesn't exist.

File sourceDir = new File(source); //this directory already exists
File destDir = new File(dest); //this is a new directory
destDir.mkdirs(); // make sure that the dest directory exists

Path destPath = destDir.toPath();
for (File sourceFile : sourceDir.listFiles()) {
    Path sourcePath = sourceFile.toPath();
    Files.copy(sourcePath, destPath.resolve(sourcePath.getFileName()));
}

Note that sourceDir.listFiles() will also return directories, which you will either want t recurse into, or ignore...

RudolphEst
  • 1,240
  • 13
  • 21
  • Thanks, but unfortunately this still throws the same error. I've even run `destDir.exists()`, which returns `True`. It almost sounds like it thinks `destDir/Experiment.log` should be a directory. Is that not the case, though? – Adam_G Feb 28 '13 at 16:58
  • I ran the code on by side and it works, as long as there are no sub directories in the directory I am trying to copy... – RudolphEst Feb 28 '13 at 17:28
  • The only difference I can think of is, I need to use `for (int i = 0; i < trainingFilesSize; i++) { Path vectorPath = vectorDir.listFiles()[i].toPath();` because I'll be copying certain indices. This shouldn't make a difference, though, right? – Adam_G Feb 28 '13 at 17:53
  • You're right ... it shouldn't make a difference. I am confused. – RudolphEst Mar 01 '13 at 11:38
1

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);
}
Downhillski
  • 2,555
  • 2
  • 27
  • 39
0
for (int i = 0; i < sourceSize; i++) {
    Path sourcePath = sourceDir.listFiles()[i].toPath();
    Files.copy(sourcePath, destPath.resolve(sourcePath.getFileName()));
}

This is very strange code. You have already got a file count from somewhere, in sourceSize, yet you are calling listFiles() for every iteration. I would have expected something more like this:

for (File file : sourceDir.listFiles()) {
    Path sourcePath = file.toPath();
    Files.copy(sourcePath, destPath.resolve(sourcePath.getFileName()));
}
user207421
  • 305,947
  • 44
  • 307
  • 483
  • For reasons that fall outside the scope of this question, I need to use the `int i = 0`... format. It has to do with selecting random numbers, and passing off the index number. – Adam_G Mar 01 '13 at 12:30
  • 1
    You've missed the point completely. That's not a reason to keep calling listFiles() on every iteration. You are running the risk of the directory changing between iterations, and of processing the same file twice. – user207421 Mar 01 '13 at 21:20