Java Path Delete nio deleteFile(Path path)

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

Description

Deletes a file if it exists.

License

Open Source License

Parameter

Parameter Description
path the path to the file to delete.

Exception

Parameter Description
IOException if something goes wrong.

Declaration

public static void deleteFile(Path path) throws IOException 

Method Source Code


//package com.java2s;
import java.io.IOException;

import java.nio.file.*;

public class Main {
    /**/*from   ww w. j  a  va2s . c o m*/
     * Deletes a file if it exists.
     *
     * @param path the path to the file to delete.
     * @throws IOException if something goes wrong.
     */
    public static void deleteFile(Path path) throws IOException {
        if (!isFolder(path)) {
            Files.deleteIfExists(path);
        }
    }

    /**
     * Tests whether a file is a directory.
     *
     * @param path the path to the file to test.
     * @return true if the file is a directory; false if the file does not
     * exist, is not a directory, or it cannot be determined if the file
     * is a directory or not.
     */
    public static boolean isFolder(Path path) {
        return Files.isDirectory(path);
    }
}

Related

  1. deleteAllFilesRecursively(Path path)
  2. deleteContent(Path directory)
  3. deleteEmptyDirsUpTo(Path from, Path to)
  4. deleteEmptyParentDirs(Path pkgDirPath, Path repoPath)
  5. deleteFile(Path p)
  6. deleteFile(Path path)
  7. deleteFile(Path path)
  8. deleteFile(String filePath)
  9. deleteFile(String path)