1

我需要在java中自动设置文件属性的值。我找到了方法

Path Files.setAttribute(Path path, String attribute, Object value, LinkOption... options)

但文档未指定该值将以原子方式设置。请告诉我该怎么做。

4

1 回答 1

1

FileLock 可能是一个解决方案:

    Path path = new File("C:/Test/test.txt").toPath();

    FileChannel fc = FileChannel.open(path, StandardOpenOption.WRITE);
    FileLock lock = fc.tryLock();
    if (lock != null) {
        try {
            FileTime fileTime = FileTime.fromMillis(0);
            Files.setAttribute(path, "basic:creationTime", fileTime, LinkOption.NOFOLLOW_LINKS);
        } finally {
            lock.release();
        }
    }
于 2014-11-30T15:03:12.433 回答