What is the difference between these two interfaces? BasicFileAttributes vs BasicFileAttributeView
I understand that they serve to retrieve basic metadata from a file, but what actually differs from each other?
EDIT: I previously meant that in the following example the 2 interfaces are used interchangeably. Is there any difference at all? besides the fact that in order to access the attributes with the view first you have to call the .readAttributes()
method?
BasicFileAttributeView bs = Files.getFileAttributeView(path, BasicFileAttributeView.class); BasicFileAttributes bfa = Files.readAttributes(path, BasicFileAttributes.class);
Advertisement
Answer
Interfaces are nothing but the signatures they describe. So the difference between these two interfaces are, that they demand methods of other signatures implemented.
If you have a BasicFileAttributeView instance, you can get a BasicFileAttributes using readAttributes()
. If you don’t have a BasicFileAttributeView (BFAV) instance, you can get it using Files.getFileAttributeView. It is guaranteed that you can pass BFAV, even if it may not work with every subclass of FileAttributeView.
Example:
BasicFileAttributeView bfav = Files.getFileAttributeView( FileSystems.getDefault().getPath("/dev/null"), BasicFileAttributeView.class ); BasicFileAttributes bfa = bfav.readAttributes(); System.out.println(bfa.lastAccessTime());
- We get the default FileSystem, so that we can use it in the next step.
- We get a Path using the FileSystem, so that we can use it in the next step
- We get the BasicFileAttributeView (which represents the ability to read a BasicFileAttribute) using the Path, so that …
- We get the BasicFileAttribute using the BasicFileAttributeView, so that …
- We get the lastAccessTime (a FileTime), …
- We print it using FileTime::toString