Pages Navigation Menu

Coding is much easier than you think

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();
      }
   }
}

5 Comments

  1. I think you have noted some very interesting details , thankyou for the post.

  2. I just want to tell you that I’m beginner to weblog and seriously enjoyed your blog site. Very likely I’m planning to bookmark your website . You surely come with remarkable articles and reviews. Bless you for sharing your web site.

  3. You can find some intriguing points in time in this write-up but I do not know if I see all of them center to heart. There is certainly some validity but I will take hold opinion until I look into it further. Great post , thanks and we want more! Added to FeedBurner as well

  4. Youre so cool! I dont suppose Ive read something like this ahead of. So nice to discover somebody with some original thoughts on this subject. realy thank you for starting this up. this web-site is some thing which is required on the web, someone having a little originality. useful job for bringing some thing new to the net!

  5. Nice…thanks for this.