Files usually contain information like its creation time, last modified time, last accessed time, size, type, permissions, ownership and other details which constitute its metadata. NIO.2 enhancements in Java 7 adds views to obtain metadata information of the file. The file attributes are grouped into different views based on their relatedness and applicability, for example general file attributes like size, last modified time, last accessed time, creation time are grouped as one and other Unix based permissions information in grouped into one view and so on.
also read:
Various views supported in NIO.2 are:
- BasicFileAttributeView: A file attribute view that provides a view of a basic set of file attributes common to many file systems. The basic set of file attributes consist of mandatory and optional file attributes as defined by the BasicFileAttributes interface. This view is named as “basic”
- DosFileAttributeView: Extends the BasicFileAttributeView and adds the four supported attributes on file systems that support the DOS attributes. This view is named as “dos”.
- PosixFileAttributeView: This extends the BasicFileAttributeView with attributes supported on file systems that supports POSIX standards such as Unix. This view is named as: “posix”
- UserDefinedFileAttributeView: One can specify custom attributes using this view.
- AclFileAttributeView: This view supports obtaining file’s access control list. It is named as “acl”.
- FileOwnerAttributeView: Any file system that supports file owners would provide this view. This is used to obtain the owner information for the file. This view is named as “owner”.
For each of the views I have also mentioned the view names and its important to know these view names because one can refer to the attributes using [view-name]:attributeName. Before you access the views you need to make sure that the file/file system/file store supports the view. For example On Windows OS you cannot find PosixFileAttributeView and on Unix no DosFileAttributeView. Lets see how one can find out the view supported. One can find out the views supported per file or even at the FileStore level.
FileSystem fileSystem = FileSystems.getDefault(); for(FileStore store : fileSystem.getFileStores()){ System.out.println(store.toString()); for (String view : fileSystem.supportedFileAttributeViews()){ if ( store.supportsFileAttributeView(view)){ System.out.print(view+","); } } System.out.println(""); }
On my machine the output for the above code is:
Windows (C:) acl,basic,owner,user,dos ImptStuff (D:) acl,basic,owner,user,dos Entertainment (E:) acl,basic,owner,user,dos
FileStore gives a list of all partitions on my file system and then for each partition find out the views supported.
Going ahead we would pick one file and then find out the attributes associated with the file.
Basic Attributes view
A basic attributes view would provide: creation time, last modified time, last accessed time, size, file type, file key. All the views have an associated attributes class like BasicFileAttributeView has BasicFileAttrbutes class which can be used to obtain the values of these attributes.
/* Attributes of Basic View */ Path path1 = Paths.get("test1"); BasicFileAttributes basicAttributes = Files.readAttributes(path1, BasicFileAttributes.class); System.out.println("Creation Time: "+basicAttributes.creationTime()); System.out.println("Directory? "+basicAttributes.isDirectory()); System.out.println("File? "+basicAttributes.isRegularFile()); System.out.println("Last accessed on: "+basicAttributes.lastAccessTime()); System.out.println("Last modified on: "+basicAttributes.lastModifiedTime()); System.out.println("File size: "+basicAttributes.size()+"bytes");
The output for the above code:
Creation Time: 2012-06-20T17:32:09.703125Z Directory? false File? true Last accessed on: 2012-07-12T17:00:28.234Z Last modified on: 2012-07-12T17:00:28.234Z File size: 61bytes
Files is the class which is used for all the operations related to file attributes- getting and setting their values. One can even use getAttribute method of Files class to get the required attribute:
FileTime lastModifiedTime = (FileTime)Files.getAttribute(path1, "basic:lastModifiedTime");
One can even modfiy the attribute values. I am not sure if other values can be modified but I was able to modify the time related information. Again you make use of Files class to modify the attribute values.
/* Modifying file attribute */ FileTime newTime = FileTime.fromMillis(System.currentTimeMillis()); Files.setLastModifiedTime(path1, newTime); BasicFileAttributeView bAttributesView = Files.getFileAttributeView(path1, BasicFileAttributeView.class); bAttributesView.setTimes(null, newTime, null);
One can make use of Files.setAttribute() to modify the attribute. In the above example we used setLastModifiedTime for modifying the lastModifiedTime attribute, then we made use of BasicFileAttributeView to modify the creation, last modified, and last access attributes by using the setTimes method which takes in 3 parameters representing the last modified, last access time, and creation time as instances of FileTime class.
This was a quick overview of the Basic file attributes. One can explore the other views to find out the attributes supported by that view. Pro Java 7 NIO.2 addresses the three primary elements that offer new input/output (I/O) APIs in Java 7, giving you the skills to write robust, scalable Java applications.