publicclass SymlinkTime { publicstaticvoid main(String[] args) throws IOException {
Path dir = TestUtil.createTemporaryDirectory(); if (!TestUtil.supportsLinks(dir)) {
System.out.println("Links not supported: skipping test"); return;
}
try { // Create file and symbolic link to it final Path file = dir.resolve("file"); final Path link = dir.resolve("link");
Files.createFile(file); try { // Delay creating the link to get different time attributes Thread.currentThread().sleep(5000);
} catch (InterruptedException ignored) {
}
Files.createSymbolicLink(link, file);
// Save file modification and access times
BasicFileAttributeView view = Files.getFileAttributeView(link,
BasicFileAttributeView.class);
BasicFileAttributes attr = view.readAttributes();
printTimes("Original file times", attr);
FileTime fileModTime = attr.lastModifiedTime();
FileTime fileAccTime = attr.lastAccessTime();
// Read link modification and access times
view = Files.getFileAttributeView(link,
BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
attr = view.readAttributes();
printTimes("Original link times", attr);
// Set new base time and offset increment long base = 1000000000000L; // 2001-09-09T01:46:40Z long delta = 1000*60L;
// Set new link modification and access times
FileTime linkModTime = FileTime.fromMillis(base + delta);
FileTime linkAccTime = FileTime.fromMillis(base + 2L*delta);
view.setTimes(linkModTime, linkAccTime, null);
// Verify link modification and access times updated correctly
view = Files.getFileAttributeView(link,
BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
attr = view.readAttributes();
printTimes("Updated link times", attr);
check("Link", attr, linkModTime, linkAccTime);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.