In this post, we will cover Java NIo2 File Attributes. File attributes are also known as metadata. Metadata can be used to find out information about the file systems like if it’s a regular file or a directory, who is the owner of the file or directory.
File metadata can be used to get information about the file ranging from the size of the file to the access permissions. These File Attributes can be used to check critical information before moving ahead with real file-based operations.
I will be covering different metadata attributes in this post.
To get information about all the basic attributes of the file, we can use
Files.readAttribute() method. This method will return
BasicFileAttributes which can be used to access various basic file attributes.This approach is more efficient as we are not required to make a separate call to the underlying file system to read individual attributes.
We will get
BasicFileAttributes for a given path and then check what all information we can get from this
Path path = Paths.get("/Users/umesh/personal/tutorials/source"); BasicFileAttributeView fileAttributeView = Files.getFileAttributeView(path, BasicFileAttributeView.class); BasicFileAttributes basicFileAttributes = fileAttributeView.readAttributes();
We first retrieved
BasicFileAttributeView and retrieved.
BasicFileAttributes from it.We went this route since
BasicFileAttributeView contains set of file attributes consist of mandatory and optional file attributes as defined by the
Once we have BasicFileAttribute, we can get all basic file attribute information from it.
To find creation time of the file, use creationTime() method
We can also check if a given path is a file or directory
Here is complete example demonstrating various methods available to get basic file information.
Path path = Paths.get("/Users/umesh/personal/tutorials/source"); BasicFileAttributeView fileAttributeView = Files.getFileAttributeView(path, BasicFileAttributeView.class); BasicFileAttributes basicFileAttributes = fileAttributeView.readAttributes(); System.out.println("creationTime: " + basicFileAttributes.creationTime()); System.out.println("lastAccessTime: " + basicFileAttributes.lastAccessTime()); System.out.println("lastModifiedTime: " + basicFileAttributes.lastModifiedTime()); System.out.println("isDirectory: " + basicFileAttributes.isDirectory()); System.out.println("isOther: " + basicFileAttributes.isOther()); System.out.println("isRegularFile: " + basicFileAttributes.isRegularFile()); System.out.println("isSymbolicLink: " + basicFileAttributes.isSymbolicLink()); System.out.println("size: " + basicFileAttributes.size());
All method related to getting time returns
FileTime object which is different than simple
In order to get high-level file ownership information, We can use
FileOwnerAttributeView provided by Java NIO2 API.
FileOwnerAttributeView fileOwner=Files.getFileAttributeView(path, FileOwnerAttributeView.class); System.out.println("File Owner Name " +fileOwner.getOwner());
You can get information about the POSIX or DOS file attributes by passing
Java NIO2 API provides a convenient FileStore class to get information about the file. We can use this class to get information about the underlying file system like how much space is available, how much used etc.
FileStore store = Files.getFileStore(path); long total = store.getTotalSpace() / 1024; long used = (store.getTotalSpace() - store.getUnallocatedSpace()) /1024; long available = store.getUsableSpace() /1024;
In this post, we covered basics as how to get information about the basic file attributes using Java 7 NIO2 File Attributes.
All the code of this article is available Over on Github. This is a Maven-based project.
Hello!! Welcome to the Java Development Journal. We love to share our knowledge with our readers and love to build a thriving community.