Simulate touch command with Java

后端 未结 7 1379
太阳男子
太阳男子 2021-02-03 17:33

I want to change modification timestamp of a binary file. What is the best way for doing this?

Would opening and closing the file be a good option? (I require a solution

7条回答
  •  醉酒成梦
    2021-02-03 17:53

    I know Apache Ant has a Task which does just that.
    See the source code of Touch (which can show you how they do it)

    They use FILE_UTILS.setFileLastModified(file, modTime);, which uses ResourceUtils.setLastModified(new FileResource(file), time);, which uses a org.apache.tools.ant.types.resources.Touchable, implemented by org.apache.tools.ant.types.resources.FileResource...

    Basically, it is a call to File.setLastModified(modTime).

提交回复
热议问题