Moves the given input to the given directory. - Java java.nio.file

Java examples for java.nio.file:Path

Description

Moves the given input to the given directory.

Demo Code


//package com.java2s;

import java.io.IOException;

import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;

import java.nio.file.StandardCopyOption;

import java.util.Iterator;

public class Main {
    /**/*from  w  w  w.ja  va2 s.  c o m*/
     * Moves the given input to the given directory. If the input is a directory,
     * all children files/directories will be moved to the target directory and
     * the input directory will be deleted.
     */
    public static void move(Path file, Path outputDir) throws Exception {
        if (Files.isRegularFile(file)) {
            Files.createDirectories(outputDir);
            Files.move(file,
                    outputDir.resolve(file.getFileName().toString()),
                    StandardCopyOption.REPLACE_EXISTING);
        } else if (Files.isDirectory(file)) {
            Files.createDirectories(outputDir);
            DirectoryStream<Path> files = Files.newDirectoryStream(file);
            Iterator<Path> filesItr = files.iterator();
            while (filesItr.hasNext()) {
                Path f = filesItr.next();
                Path target = outputDir.resolve(f.getFileName().toString());

                if (Files.isDirectory(target)) {
                    cleanDirectory(target);
                }

                Files.move(f, target, StandardCopyOption.REPLACE_EXISTING);
            }
            files.close();
            Files.delete(file);
        }
    }

    /**
     * Cleans the given directory without deleting it.
     */
    public static void cleanDirectory(Path directory) throws IOException {
        if (!Files.exists(directory)) {
            String message = directory + " does not exist";
            throw new IllegalArgumentException(message);
        }

        if (!Files.isDirectory(directory)) {
            String message = directory + " is not a directory";
            throw new IllegalArgumentException(message);
        }

        DirectoryStream<Path> directoryStream = Files
                .newDirectoryStream(directory);
        IOException exception = null;
        for (Path p : directoryStream) {
            try {
                delete(p);
            } catch (IOException ioe) {
                exception = ioe;
            }
        }
        directoryStream.close();

        if (exception != null) {
            throw exception;
        }
    }

    /**
     * Deletes a file or directory. A directory does not have to be empty.
     */
    public static void delete(Path fileOrDirectory) throws IOException {
        if (Files.isDirectory(fileOrDirectory)) {
            deleteDirectory(fileOrDirectory);
        } else {
            Files.deleteIfExists(fileOrDirectory);
        }
    }

    /**
     * Deletes a directory and its content recursively.
     */
    public static void deleteDirectory(Path directory) throws IOException {
        if (!Files.exists(directory)) {
            return;
        }

        cleanDirectory(directory);
        Files.delete(directory);
    }
}

Related Tutorials