How to delete file in Java
Published: June 2, 2010 , Updated: May 30, 2010 , Author: mkyong
No nonsense, just issue the File.delete() to delete a file, it will return a boolean value to indicate the delete operation status; true if the file is deleted; false if failed.
Example
In this example, it will delete a log file named “c:\\logfile20100131.log”.
package com.mkyong.file; import java.io.File; public class DeleteFileExample { public static void main(String[] args) { try{ File file = new File("c:\\logfile20100131.log"); if(file.delete()){ System.out.println(file.getName() + " is deleted!"); }else{ System.out.println("Delete operation is failed."); } }catch(Exception e){ e.printStackTrace(); } } }







this code is not working because you didn’t used file.createNewFile without this file is not created so how can it deleted
please correct it . if i am wrong please inform me through mail
thank you
This is demonstrate how to “delete” an existing file, not create.