This is the code I have written to delete a file if it already exists
public void createFile(Map<String, String> map, String name)
{
try
{
System.out.println("Creating new File...");
File file = new File("./Analysis/files/master.csv");
if (file.exists())
{
System.out.println("File Deleted...."+file.delete());
}
System.out.println("New File Created "+file.createNewFile());
FileWriter fw = new FileWriter(file, true);
for (Map.Entry<String, String> entry : map.entrySet())
{
fw.write(entry.getKey());
fw.write(",");
fw.write(entry.getValue());
fw.write("\n");
fw.flush();
}
fw.close();
}catch(IOException e)
{
throw new BuildException(e.getMessage());
}
}
This file.exists is showing false for the file which already exists in that path so its not deleting that file and append the contents to that file.Any idea?