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