Get Bulk Attributes with readAttributes() - Java File Path IO

Java examples for File Path IO:File Attribute

Introduction

You can extract attributes in bulk using the readAttributes() method.

Demo Code

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

public class Main {
  public static void main(String[] args) {

    BasicFileAttributes attr = null;
    Path path = Paths.get("D:\\folder0\\Java\\JDK7\\folder5\\code\\folder1\\folder2\\folder6", "test.txt");

    //extract attributes as bulk with readAttributes
    try {/* w  w w  . j  a  va2s. c  o m*/
        attr = Files.readAttributes(path, BasicFileAttributes.class);
    } catch (IOException e) {
        System.err.println(e);
    }

    System.out.println("File size: " + attr.size());
    System.out.println("File creation time: " + attr.creationTime());
    System.out.println("File was last time accessed at: " + attr.lastAccessTime());
    System.out.println("File was last time modified at: " + attr.lastModifiedTime());

    System.out.println("Is directory ? " + attr.isDirectory());
    System.out.println("Is regular file ? " + attr.isRegularFile());
    System.out.println("Is symbolic link ? " + attr.isSymbolicLink());
    System.out.println("Is other ? " + attr.isOther());
  }
}

Related Tutorials