Following example shows how to delete file using file.delete() method in Java.
delete
public boolean delete()
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.Note that the Files class defines the delete method to throw an IOException when a file cannot be deleted. This is useful for error reporting and to diagnose why a file cannot be deleted.
Returns:
true if and only if the file or directory is successfully deleted; false otherwise
Throws:
SecurityException - If a security manager exists and its SecurityManager.checkDelete(java.lang.String) method denies delete access to the file
Sample Program
DeleteFile.java
Result
The above code will delete the file and produce the following output if file exist in specified directory.
delete
public boolean delete()
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.Note that the Files class defines the delete method to throw an IOException when a file cannot be deleted. This is useful for error reporting and to diagnose why a file cannot be deleted.
Returns:
true if and only if the file or directory is successfully deleted; false otherwise
Throws:
SecurityException - If a security manager exists and its SecurityManager.checkDelete(java.lang.String) method denies delete access to the file
Sample Program
DeleteFile.java
package com.javatutorialcorner.file; import java.io.File; public class DeleteFile { public static void main(String[] args) { String path = "/home/annamalai/workspace/File/jtc.txt"; File jtcFile = new File(path); boolean isDeleted = jtcFile.delete(); if (isDeleted) { System.out.println("The file has been successfully deleted"); } } }
Result
The above code will delete the file and produce the following output if file exist in specified directory.
The file has been successfully deleted
0 comments:
Post a Comment