Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to delete file in Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 219
    Comment on it

    Hello readers,

    To Delete a File in Java is not a big issue, you don't need to do any rocket science for this.

    So if you want to delete the file in java here below is the method.

    To delete the file, you just have to issue the File.delete(), which will return a boolean value to indicate the delete operation status.
    If the file is deleted it will indicate true else indicate false if failed.

    Example-

    package com.FindNerd.DAO;
    
    import java.io.File;
    
    public class DeleteFile {
    
    public static void main(String[] args)
        { 
         try{
     //Create file that you want to delete
    File file = new File("c:\\deletemyfile.log");
    
      if(file.delete()){
       System.out.println(file.getName() + " file has been deleted!");
      }else{
       System.out.println("Delete operation has failed.");
      }
    
     }catch(Exception e){
    
      e.printStackTrace();
    
     }
    
    }
    }
    

    In above example,

    The file which I want to delete from my drive is deletemyfile.log which is locating in c drive, so I have issued File.Delete() to delete a log file named c:\\deletemyfile.log".

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: