Simulate touch command with Java

后端 未结 7 1444
太阳男子
太阳男子 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:56

    Since File is a bad abstraction, it is better to use Files and Path:

    public static void touch(final Path path) throws IOException {
        Objects.requireNotNull(path, "path is null");
        if (Files.exists(path)) {
            Files.setLastModifiedTime(path, FileTime.from(Instant.now()));
        } else {
            Files.createFile(path);
        }
    }
    

提交回复
热议问题