Monday, March 11, 2013

Java / Delete file, directory


How to delete file or direcory in Java?
Use File class and delete() method.
This method deletes file or directory if exists and returning true on success otherwise false.

import java.io.File;

public class DeleteFile 
{
 public static void main(String[] args) 
 {
  File file = new File( "readme.txt" );
  boolean result = file.delete();
  
  if( result == true )
   System.out.println( "File deleted" );
  else
   System.out.println( "File doesn't exists" );
 }
}

No comments: