Maintaining basic file attributes using the BasicFileAttributeView - Java File Path IO

Java examples for File Path IO:File Attribute

Description

Maintaining basic file attributes using the BasicFileAttributeView

Demo Code

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

public class Main {
  public static void main(String[] args) {
    Path path = FileSystems.getDefault().getPath("/home/docs/users.txt");
    try {/*from w w w  .  j ava 2 s .  c  o  m*/
      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");
    }
  }
}

Related Tutorials