We are moved to new domain
Click -> www.ehowtonow.com
Thursday, 14 May 2015

How to delete file using Java

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
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

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to delete file using Java Rating: 5 Reviewed By: eHowToNow