Java Path Remove nio rm(final LinkedHashMap unremoved, Path... locations)

Here you can find the source of rm(final LinkedHashMap unremoved, Path... locations)

Description

rm

License

Apache License

Declaration

private static LinkedHashMap<Path, Throwable> rm(final LinkedHashMap<Path, Throwable> unremoved,
            Path... locations) 

Method Source Code

//package com.java2s;
/*//w ww.jav a 2  s .  co  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;

import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;

import java.nio.file.attribute.BasicFileAttributes;

import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
    /**
     * Deletes one or more files or directories (and everything underneath it).
     * 
     * @throws IOException if any of the given files (or their subhierarchy files in case
     * of directories) cannot be removed.
     */
    public static void rm(Path... locations) throws IOException {
        LinkedHashMap<Path, Throwable> unremoved = rm(new LinkedHashMap<Path, Throwable>(), locations);
        if (!unremoved.isEmpty()) {
            StringBuilder b = new StringBuilder(
                    "Could not remove the following files (in the order of attempts):\n");
            for (Map.Entry<Path, Throwable> kv : unremoved.entrySet()) {
                b.append("   ").append(kv.getKey().toAbsolutePath()).append(": ").append(kv.getValue())
                        .append("\n");
            }
            throw new IOException(b.toString());
        }
    }

    private static LinkedHashMap<Path, Throwable> rm(final LinkedHashMap<Path, Throwable> unremoved,
            Path... locations) {
        if (locations != null) {
            for (Path location : locations) {
                // TODO: remove this leniency!
                if (location != null && Files.exists(location)) {
                    try {
                        Files.walkFileTree(location, new FileVisitor<Path>() {
                            @Override
                            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                                    throws IOException {
                                return FileVisitResult.CONTINUE;
                            }

                            @Override
                            public FileVisitResult postVisitDirectory(Path dir, IOException impossible)
                                    throws IOException {
                                assert impossible == null;

                                try {
                                    Files.delete(dir);
                                } catch (IOException e) {
                                    unremoved.put(dir, e);
                                }
                                return FileVisitResult.CONTINUE;
                            }

                            @Override
                            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                                    throws IOException {
                                try {
                                    Files.delete(file);
                                } catch (IOException exc) {
                                    unremoved.put(file, exc);
                                }
                                return FileVisitResult.CONTINUE;
                            }

                            @Override
                            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                                if (exc != null) {
                                    unremoved.put(file, exc);
                                }
                                return FileVisitResult.CONTINUE;
                            }
                        });
                    } catch (IOException impossible) {
                        throw new AssertionError("visitor threw exception", impossible);
                    }
                }
            }
        }
        return unremoved;
    }
}

Related

  1. removeLine(String path, String line, boolean commentAware)
  2. removeRecursive(Path path)
  3. removeRecursive(Path path)
  4. removeSymlink(Path softLinkLocation)
  5. reportRemoveSymink(Path softLinkLocation)