Files.getFileAttributeView and Files.getFileStore in Java | java.nio.file API
November 29, 2013
Files.getFileAttributeView and Files.getFileStore has been introduced in jdk 7. Files class is related to NIO 2. Files class has many important methods. In this page I will cover Files.getFileAttributeView and Files.getFileStore.
Files.getFileAttributeView in java.nio.file
java.nio.file.Files keep the method getFileAttributeView(). The role of this method is to fetch all attributes of a file in read mode or updatable mode. Files.getFileAttributeView returns java.nio.file.attribute.AclFileAttributeView. The syntax of the method is as below.public static <V extends FileAttributeView> V getFileAttributeView(Path p, Class<AclFileAttributeView> type, LinkOption... opts)
GetFileAttributeViewExample.java
package com.concretepage.io.file; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.AclEntry; import java.nio.file.attribute.AclFileAttributeView; import java.util.List; public class GetFileAttributeViewExample { public static void main(String[] args) throws IOException { Path path = Paths.get("D:/cp","file.txt"); AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class); if (view != null) { List<AclEntry> acls = view.getAcl(); for(AclEntry acl: acls){ System.out.println(acl.principal().getName()+":"+acl.type()); } } } }
Output
BUILTIN\Administrators:ALLOW NT AUTHORITY\SYSTEM:ALLOW
Files.getFileStore in java.nio.file
java.nio.file.Files has so many useful methods. Files.getFileStore is one of them. It returns the store information of a given file. Files.getFileStore returns FileStore and FileStore has methods to get the information of file location like space being used, free space etc.FileStore in Java NIO
A java.nio.file.FileStore represents the store for a file. The store can be a device, volume, disk partition or any external storage device. To instantiate FileStore, we need to call Files.getFileStore()Find the syntax of Files.getFileStore.
public static FileStore getFileStore(Path p)
GetFileStoreExample.java
package com.concretepage.io.file; import java.io.IOException; import java.nio.file.FileStore; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class GetFileStoreExample { public static void main(String[] args) throws IOException { Path path = Paths.get("D:/cp","file.txt"); FileStore fileStore = Files.getFileStore(path); System.out.println(fileStore.getTotalSpace()); System.out.println(fileStore.getUnallocatedSpace()); System.out.println(fileStore.getUsableSpace()); } }