Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date Created is not going to change while delete file and then create file

Tags:

java

file

due to some reason i have to remove older file and create new file according to our client

Date Modified is change to current time but Date Created is not change to 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 remove older data and write new data

but why date created time is not changed..

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

like image 631
Darshan Patel Avatar asked Jan 02 '14 13:01

Darshan Patel


People also ask

Does copying a file change the date modified?

If you copy a file from C:\fat16 to D:\NTFS, it keeps the same modified date and time but changes the created date and time to the current date and time. If you move a file from C:\fat16 to D:\NTFS, it keeps the same modified date and time and keeps the same created date and time.

Can you change the date modified on a folder?

Unfortunately it's not possible to manually change the modified date of any file in Windows.

How do I change the date modified on Google Drive?

If you want to change the last modified date or change the file creation data, press to enable the Modify date and time stamps checkbox. This will enable you to change the created, modified, and accessed timestamps—change these using the options provided.


1 Answers

This is happening due to File System Tunneling in windows. There are options to disable/configure it. You can get more info regarding this from this link support.microsoft.com.

Innorder to counteract to this in java way, you can set file created time(file attribute) immediately after creation of 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.

like image 158
Sinto Avatar answered Sep 30 '22 13:09

Sinto