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.
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.
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.
Unfortunately it's not possible to manually change the modified date of any file in Windows.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With