Java BasicFileAttributeView get file attributes

Description

Java BasicFileAttributeView get file attributes

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;

public class Main {

   public static void main(String[] args) {
      Path path = FileSystems.getDefault().getPath("Main.java");
      try {//from  w  ww .j  a v a 2 s. c o m
         BasicFileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class);
         BasicFileAttributes attributes = view.readAttributes();
         //BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
         System.out.println("Creation Time: " + attributes.creationTime());
         System.out.println("Last Accessed Time: " + attributes.lastAccessTime());
         System.out.println("Last Modified Time: " + attributes.lastModifiedTime());
         System.out.println("File Key: " + attributes.fileKey());
         System.out.println("Directory: " + attributes.isDirectory());
         System.out.println("Other Type of File: " + attributes.isOther());
         System.out.println("Regular File: " + attributes.isRegularFile());
         System.out.println("Symbolic File: " + attributes.isSymbolicLink());
         System.out.println("Size: " + attributes.size());
      } catch (IOException ex) {
         System.out.println("Attribute error");
      }
   }
}



PreviousNext

Related