创建日期不会更改,同时删除文件,然后创建文件 [英] Date Created is not going to change while delete file and then create file

查看:117
本文介绍了创建日期不会更改,同时删除文件,然后创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我必须删除旧文件,并根据我们的客户创建新文件



修改日期更改为当前时间,但创建日期不会更改为当前时间。




my代码如下:

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





此代码删除旧数据并写入新数据

但是为什么日期创建时间没有改变。



请帮我弄清楚我的代码有什么问题。
在此先感谢。

解决方案

这是由于Windows中的文件系统隧道。有选项来禁用/配置它。您可以通过此链接 support.microsoft.com 获取更多相关信息。



Innorder以java方式抵消这一点,您可以在创建文件后立即设置文件创建时间(文件属性),如下面的代码所示。

  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);


$ b public static void setFileCreationTime(String filePath)throws IOException {
Path path = Paths.get(filePath);
FileTime fileTime = FileTime.fromMillis(System.currentTimeMillis());
/ *更改创建的时间标记* /
Files.setAttribute(path,basic:creationTime,fileTime,
LinkOption.NOFOLLOW_LINKS);
}

希望这有助于您。


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.

解决方案

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.

这篇关于创建日期不会更改,同时删除文件,然后创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆