Java Path Delete nio deleteIfExists(Path thePath)

Here you can find the source of deleteIfExists(Path thePath)

Description

delete If Exists

License

Open Source License

Declaration

static public void deleteIfExists(Path thePath) 

Method Source Code

//package com.java2s;
/*//from www  .j ava  2 s.c  om
 * Copyright (c) 2013 christianr.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0.html
 * 
 * Contributors:
 *     christianr - initial API and implementation
 */

import java.io.IOException;

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

import java.nio.file.Path;

import java.nio.file.SimpleFileVisitor;

import java.nio.file.attribute.BasicFileAttributes;

public class Main {
    static public void deleteIfExists(Path thePath) {
        try {
            Files.walkFileTree(thePath, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file,
                        BasicFileAttributes attrs) throws IOException {
                    Files.deleteIfExists(file);
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir,
                        IOException exc) throws IOException {
                    Files.deleteIfExists(dir);
                    return FileVisitResult.CONTINUE;
                }

            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. deleteFile(String filePath)
  2. deleteFile(String path)
  3. deleteFilesIfExist(Path... files)
  4. deleteFilesIgnoringExceptions(final Path... files)
  5. deleteFilesRecursively(Path path, final PathMatcher pathMatcher)
  6. deleteIfExists(Path value)
  7. deleteIndexBeforeStart(String basePath)
  8. deleteLockFile(Path logFile)
  9. deleteNotEmptyDirectory(Path path)