Java Utililty Methods Path Remove nio

List of utility methods to do Path Remove nio

Description

The list of methods to do Path Remove nio are organized into topic(s).

Method

voidappendOrRemove(LinkedList paths, DirectoryStream ds)
Add path to the stack if path is directory, otherwise delete it.
for (Path p : ds) {
    if (Files.isDirectory(p))
        paths.add(p);
    else
        Files.delete(p);
voidrecursiveRemoveFolder(Path folderPath)
recursive Remove Folder
if (!folderPath.startsWith(Paths.get(TMP_DIR))) {
    throw new IllegalArgumentException("Refuse to delete a folder that is not in the system tmpdir");
Files.walkFileTree(folderPath, new FileVisitor<Path>() {
    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
        System.out.println("Deleting directory: " + dir);
        Files.delete(dir);
...
voidremoveDirectory(Path directory)
remove Directory
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
        Files.delete(file);
        return FileVisitResult.CONTINUE;
    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
...
voidremoveDirectory(Path directory)
Remove a directory and all of its contents.
final Path canonicalPath = directory.toAbsolutePath().normalize();
if (canonicalPath.equals(canonicalPath.getRoot()))
    throw new IOException("Refusing to delete root directory.");
Files.walkFileTree(canonicalPath, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.delete(file);
        return FileVisitResult.CONTINUE;
...
booleanremoveDirectory(String pathToDir)
Remove directory and all its sub-resources with specified path
return deleteRecursive(new File(pathToDir));
voidremoveDirectoryIfItIsEmpty(Path directoryToRemove)
Checks if directoryToRemove is empty itself and remove it if it is empty only.
if (Files.exists(directoryToRemove) && Files.isDirectory(directoryToRemove)
        && Files.list(directoryToRemove).collect(toList()).isEmpty()) {
    Files.delete(directoryToRemove);
PathremoveDriveLetter(Path path)
Remove the root component from the path, returning a relative path.
if (path == null) {
    return null;
if (path.isAbsolute()) {
    return Paths.get(path.getRoot().relativize(path).toString());
} else {
    return path;
voidremoveFile(final String removePath)
remove File
try {
    final Path path = Paths.get(removePath);
    Files.delete(path);
} catch (final IOException e) {
    System.err.println("Couldn't delete file. Exception: " + e);
booleanremoveFile(String path)
remove File
File fileToRemove = loadFile(path);
return fileToRemove.delete();
voidremoveFile(String workspacePath)
remove File
Path path = FileSystems.getDefault().getPath(workspacePath);
Files.delete(path);