Monday 14, Apr 2025
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

Related Posts:

  • Java StAX Parser – Cursor API Read ExampleIn this tutorial we are going to see about StAX cursor API to read an XML document. In the Cursor example, the application instructs the parser to read the next event in the XML input stream by calling next(). Note that next(… Read More
  • How to Split the String in JavaFollowing Example shows how to split the string using str.split(String pattern) method in Java. Sample Programpackage com.javatutorialcorner.javastring;public class SplitString { public static void main(String[] args) { … Read More
  • How to add value to all types of Collections Following example shows how to add values in all types collections in Java. Sample Program AddValueToCollection.java package com.javatutorialcorner.collection; import java.util.ArrayList; import java.util.Collection; impo… Read More
  • How to change File Last modified Date using Java Following example shows how to change file last modified date using file.setLastModified() method in Java. setLastModifiedpublic boolean setLastModified(long time)  Sets the last-modified time of the file or directory … Read More
  • How to convert a String to Lower caseFollowing example shows how to convert a string to lower case. Sample Programpackage com.javatutorialcorner.javastring;public class ToLowerCase { public static void main(String[] args) { String input = "Java tutoria… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

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