Java Path Create nio createOverwriteDirectory(Path path)

Here you can find the source of createOverwriteDirectory(Path path)

Description

Creates new directory or overwrites existing one.

License

Open Source License

Parameter

Parameter Description
path path to the directory.

Exception

Parameter Description
IOException thrown on filesystem error.

Declaration

public static void createOverwriteDirectory(Path path) throws IOException 

Method Source Code


//package com.java2s;
// License as published by the Free Software Foundation; either

import java.io.IOException;

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

public class Main {
    /**/*ww  w  .j a v  a2s . c  o  m*/
     * Creates new directory or overwrites existing one.
     *
     * @param path
     *        path to the directory.
     * @throws IOException
     *         thrown on filesystem error.
     */
    public static void createOverwriteDirectory(Path path) throws IOException {
        if (Files.exists(path)) {
            delete(path);
        }
        Files.createDirectory(path);
    }

    /**
     * Deletes the file, if it is a directory, deletes its content recursively.
     *
     * @param root
     *        path to the file.
     * @throws IOException
     *         thrown on filesystem error.
     */
    public static void delete(Path root) throws IOException {
        if (Files.isDirectory(root)) {
            final DirectoryStream<Path> subPaths = Files.newDirectoryStream(root);
            for (Path path : subPaths) {
                delete(path);
            }
            subPaths.close();
            Files.delete(root);
        } else {
            Files.delete(root);
        }
    }
}

Related

  1. createJarFile(Path directory, String name, Optional manifest, Class[] classesToAdd, Map filesToAdd)
  2. createJSONFileIfNotExists(Path path)
  3. createLanguageStructure(final String lang, final Path docRootFolder)
  4. createList(Iterable dirs)
  5. createMainClassAndBuildFileWithDeps(String targetName, String deps, Path dir)
  6. createPath(String fileName, String subfolder)
  7. createPathComparator()
  8. createPathMatcher(String[] patterns)
  9. createPathOrNull(String pathString)