Renames the given path to the given new name. - Java File Path IO

Java examples for File Path IO:Path

Description

Renames the given path to the given new name.

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. jav  a  2 s  .c  o m
     * Renames the given path to the given new name. Existing files / directories
     * will be replaced.
     */
    public static Path rename(Path path, String name) throws IOException {
        Path target = path.resolveSibling(name);

        // If target is a directory, we have to delete it, because move will not
        // overwrite an existing directory (but an existing file).
        if (Files.isDirectory(target)) {
            deleteDirectory(target);
        }

        return Files
                .move(path, target, StandardCopyOption.REPLACE_EXISTING);
    }

    /**
     * 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);
    }

    /**
     * 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);
        }
    }
}

Related Tutorials