Java Program to check for the supported attribute via java.nio.file.FileStore

Java 8Object Oriented ProgrammingProgramming

Following is our file:

Path p = Paths.get("E:/input.txt");
FileStore file = Files.getFileStore(p);

Now, check for the supported attributes one by one:

FileAttributeView = file.supportsFileAttributeView(FileAttributeView.class)
PosixFileAttributeView = file.supportsFileAttributeView(PosixFileAttributeView.class)
BasicFileAttributeView = file.supportsFileAttributeView(BasicFileAttributeView.class)

Example

import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.FileAttributeView;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.PosixFileAttributeView;
public class Demo {
   public static void main(String[] args) throws Exception {
      Path p = Paths.get("E:/input.txt");
      FileStore file = Files.getFileStore(p);
      System.out.println("FileAttributeView = " + file.supportsFileAttributeView(FileAttributeView.class));
      System.out.println("PosixFileAttributeView = "+ file.supportsFileAttributeView(PosixFileAttributeView.class));
      System.out.println("BasicFileAttributeView = "+ file.supportsFileAttributeView(BasicFileAttributeView.class));
      System.out.println("FileOwnerAttributeView supported = "+ file.supportsFileAttributeView(FileOwnerAttributeView.class));
   }
}

Output

FileAttributeView = false
PosixFileAttributeView = false
BasicFileAttributeView = true
FileOwnerAttributeView supported = true
raja
Updated on 30-Jul-2019 22:30:25

Advertisements