集册 Java实例教程 将给定路径重命名为给定的新名称。

将给定路径重命名为给定的新名称。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

583
将给定路径重命名为给定新名称。
/*n o w j a   v  a . c o m - 时  代  Java 提供*/

//package com.nowjava;


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;
/** n o w    j a v a  . c o m 提供 **/

public class Main {

    /**

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

       
展开阅读全文