Pages Navigation Menu

Coding is much easier than you think

How to delete file if exists in java?

How to delete file if exists in java?

 

package com.simplecode.com;

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {
      
       try{
      
        String tempFile = "C:/mydir/myfile.txt";
        //Delete if tempFile exists
        File fileTemp = new File(tempFile);
          if (fileTemp.exists()){
             fileTemp.delete();
          }   
      }catch(Exception e){
         // if any error occurs
         e.printStackTrace();
      }
   }
}

About Mohaideen Jamil


I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

Advertisements
%d bloggers like this: