The BasicFileAttributeView Interface
The java.nio.file.attribute.BasicFileAttributeView interface defines a file attribute view for the basic set of file attributes.
interface BasicFileAttributeView extends FileAttributeView
String name()
Returns the name of the attribute view, which in this case is the string “basic”.
BasicFileAttributes readAttributes()
Reads the basic file attributes as a bulk operation. The BasicFileAttributes object can be used to read the values of the basic file attributes (p. 1330). This method is analogous to the readAttributes() method of the Files class (p. 1328).
void setTimes(FileTime lastModifiedTime,
FileTime lastAccessTime,
FileTime createTime)
Updates any or all timestamps for the file’s last modified time, last access time, and creation time attributes. If any parameter has the value null, the corresponding timestamp is not changed. Note that apart from the Files.setLast-Modified() method, there are no methods in the Files class for the last access and creation times for a directory entry.
The code below obtains a BasicFileAttributeView object at (6) that pertains to the file denoted by the path reference. A BasicFileAttributes object is obtained at (7), providing read-only access to the basic file attributes, whose values are printed by calling the printBasicFileAttributes() method. The last modified time of the directory entry is explicitly read by calling the lastModifiedTime() method of the BasicFileAttributes object.
Path path = Path.of(“project”, “src”, “pkg”, “Main.java”);
out.println(“File: ” + path);
BasicFileAttributeView bfaView = Files.getFileAttributeView(path, // (6)
BasicFileAttributeView.class);
System.out.printf(“Using view: %s%n”, bfaView.name());
// Reading the basic set of file attributes: (7)
BasicFileAttributes bfa2 = bfaView.readAttributes();
FileUtils.printBasicFileAttributes(bfa2);
FileTime currentLastModifiedTime = bfa2.lastModifiedTime();
// Updating timestamp for last modified time using view: (8)
long newLMTinMillis = currentLastModifiedTime.toMillis() + 15*60*1000L;
FileTime newLastModifiedTime = FileTime.fromMillis(newLMTinMillis);
bfaView.setTimes(newLastModifiedTime, null, null);
// Reading the updated last modified time: (9)
out.println(“updated lastModifiedTime (incorrect): “
+ bfa2.lastModifiedTime()); // (10)
out.println(“updated lastModifiedTime: “
+ Files.getLastModifiedTime(path)); // (11)
out.println(“updated lastModifiedTime: ” + Files.getAttribute(path, // (12)
“basic:lastModifiedTime”));
Possible output from the code:
File: project/src/pkg/Main.java
Using view: basic
Printing basic file attributes:
…
lastModifiedTime: 2021-07-26T15:15:46.813Z
…
updated lastModifiedTime (incorrect): 2021-07-26T15:15:46.813Z
updated lastModifiedTime: 2021-07-26T15:30:46.813Z
updated lastModifiedTime: 2021-07-26T15:30:46.813Z
The BasicFileAttributeView object allows the last modified, last access, and creation times of the directory entry to be updated by calling the setTimes() method. The code at (9) shows how the last modified time of the directory entry can be updated to a new value via the view. A FileTime object is created representing the new last modified time by first converting the current last modified time to milliseconds and incrementing it by 15 minutes. In the call to the setTimes() method, only the last modified time is specified. The other timestamps are specified as null, indicating that they should not be changed.
In order to verify the new last modified time, we might be tempted to use the current BasicFileAttributes object associated with the view, but its copies of the file attribute values are not updatable. We can create a new BasicFileAttributes object that reflects the new values of the file attributes, or alternately use the getLastModifiedTime() or the getAttribute() methods of the Files class, as shown at (11) and (12), respectively.
Leave a Reply