Get a Single Attribute with getAttribute() - Java File Path IO

Java examples for File Path IO:File Attribute

Introduction

To extract a single attribute instead of all the attributes in bulk, use the getAttribute() method.

Demo Code

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
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 a single attribute with getAttribute
    try {//from  w  w  w  . j  a  va 2  s .  c  o  m
        long size = (Long) Files.getAttribute(path, "basic:size", LinkOption.NOFOLLOW_LINKS);
        System.out.println("Size: " + size);
    } catch (IOException e) {
        System.err.println(e);
    }
  }
}

Result


Related Tutorials