You add to the file created on the specified date, it does not create a new file (which will give you the created date). What you need to do is copy the contents of the source file to something (for example, an array), delete the file, and then write a new one with all the original content plus what you want to add.
Although your description implies that this is not what you wanted this code to execute, what you mean means that you want to delete the old file and only have new content, in which case you would do the following:
public static void main(String args[]) throws Exception { File f=new File("d:\\inbox\\json\\test.txt"); if (f.exists()) { f.delete(); } f.createNewFile(); FileWriter fileWriter = new FileWriter("d:\\inbox\\json\\test.txt", false); fileWriter.write(new Date().toString()); fileWriter.close(); }
source share