Java Directory Delete nio deleteFileOrDirectory(final File source)

Here you can find the source of deleteFileOrDirectory(final File source)

Description

delete File Or Directory

License

Open Source License

Declaration

public static void deleteFileOrDirectory(final File source) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.IOException;

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

public class Main {
    public static void deleteFileOrDirectory(final File source) {
        if (source.isFile()) {
            Path path = source.toPath();
            try {
                Files.deleteIfExists(path);
            } catch (IOException | SecurityException e) {
                System.err.println(e);
            }//w w  w  . j av a  2  s.c o m
            return;
        }

        for (String element : source.list()) {
            deleteFileOrDirectory(new File(source.getPath(), element));
        }

        Path path = source.toPath();
        try {
            Files.deleteIfExists(path);
        } catch (IOException | SecurityException e) {
            System.err.println(e);
        }
    }
}

Related

  1. deleteDirIfExists(Path dirPath)
  2. deleteDirOrFile(Path p, boolean followSymLinkDir)
  3. deleteDirReqursivelyIfExists(File dir)
  4. deleteDirWithFiles(File dir, int maxDepth)
  5. deleteFileOrDirectory(File file)
  6. deleteFileOrFolder(final Path path)