The creation date will not change when you delete the file, and then create the file

for some reason I need to delete the old file and create a new file according to our client

Date Modified is a change in the current time, but the creation date does not change for the current time.

enter image description here my code is as follows

public static void main(String args[]) throws Exception { FileUtils.deleteQuietly(new File("d:\\inbox\\json\\test.txt")); FileWriter fileWriter = new FileWriter("d:\\inbox\\json\\test.txt", false); fileWriter.append(new Date().toString()); fileWriter.close(); } 

this code deletes old data and writes new data

but why the creation date does not change.

please help me figure out what's wrong with my code. thanks in advance.

+6
source share
8 answers

This is due to file system tunneling in windows. There are options to disable / configure it. You can get more information about this at support.microsoft.com .

In Innorder, to counteract this in the java method, you can set the file creation time (file attribute) immediately after creating the file, as in the code below.

 public static void main(String args[]) throws Exception { final String FILE_PATH = "d:\\test.txt"; FileUtils.deleteQuietly(new File(FILE_PATH)); FileWriter fileWriter = new FileWriter(FILE_PATH, false); fileWriter.append(new Date().toString()); fileWriter.close(); setFileCreationTime(FILE_PATH); } public static void setFileCreationTime(String filePath) throws IOException { Path path = Paths.get(filePath); FileTime fileTime = FileTime.fromMillis(System.currentTimeMillis()); /* Changing the Created Time Stamp */ Files.setAttribute(path, "basic:creationTime", fileTime, LinkOption.NOFOLLOW_LINKS); } 

Hope this helps.

+3
source

This is an NTFS problem. The system caches information about the file, reusing it when reconstructing the file.

See the Notes section:

http://msdn.microsoft.com/en-us/library/system.io.file.getcreationtime%28v=vs.110%29.aspx

Sincerely.

PS: You can use these commands to see this without using Java code:

 del test.txt echo test > test.txt dir /TC test.txt 
+2
source

You do not delete the file before writing it.

0
source

If you do not delete the file before re-creating it, you can change it.

0
source

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

Your call to createNewFile() does not delete the existing file, it just creates a new one from one that does not exist.

You need to do:

 if (f.exists()) { f.delete(); } 
0
source

I have not tried this yet. But I believe that this is because your File object is not garbage collected.

 File f = new File("test.txt"); if(f.exists()){ f.delete(); } 

Now this is really important. You will not create a new file. This does not apply to the same memory address after you split the first.

 File f2 = new File("test.txt"); f2.write bla bla. 
0
source

I expect your code to not want to delete the file, and then just add it to the non-deleted file. To test this, I would recommend changing the code

 FileWriter fileWriter = new FileWriter("d:\\inbox\\json\\test.txt", false); fileWriter.append(new Date().toString()); fileWriter.close(); 

to

 System.out.println("Exists: " + Files.exists(Paths.get("d:\\inbox\\json\\test.txt")); FileWriter fileWriter = new FileWriter("d:\\inbox\\json\\test.txt", false); fileWriter.append(new Date().toString()); fileWriter.close(); 

If you see Exists: true in the console, why do you have a problem.

0
source

All Articles