File delete() in Java

delete(): This method is available in the java.io.File class of Java.

Syntax:

boolean java.io.File.delete()

This method deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted.

Parameters: NA

Returns: true if and only if the file or directory is successfully deleted; false otherwise.

Throws:

SecurityException - If a security manager exists and its java.lang.SecurityManager.checkDelete method denies deleting access to the file.

Approach

Java

import java.io.File;

public class Filedelete {
    public static void main(String[] args) {

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);

        System.out.println(file.delete());
    }
}

Output:

true


No comments:

Post a Comment