Copies the given input file/directory to the given directory with NIO. - Java java.nio.file

Java examples for java.nio.file:Path

Description

Copies the given input file/directory to the given directory with NIO.

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  ww . ja v  a 2  s. co  m
     * Copies the given input file/directory to the given directory. Existing
     * files / directories will be replaced.
     */
    public static void copy(Path file, Path outputDir) throws Exception {
        if (Files.isRegularFile(file)) {
            Files.createDirectories(outputDir);
            Files.copy(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.copy(f, target, StandardCopyOption.REPLACE_EXISTING);
            }
            files.close();
        }
    }

    /**
     * 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