Java - File attributes string form

Introduction

The list of attributes to read is supplied in a string form using the following syntax:

view-name:comma-separated-attributes

The view-name is the name of the attribute view to read, such as basic, posix, acl, etc.

If view-name is omitted, it defaults to basic.

If view-name is present, it is followed by a colon.

You can read all attributes of a specific view type by specifying an asterisk.

For example, you can specify "basic:*" or "*" to read all basic file attributes.

To read the size and the last modified time of the basic view, you would use "basic:size,lastModifiedTime" or "size,lastModifiedTime".

To read the owner attribute of a file using an ACL view, you would use a string "acl:owner".

To read all posix attributes of a file, you would use "posix:*".

The following code prints the size and the last modified time of the file C:\myData\Main.java.

Demo

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

public class Main {
  public static void main(String[] args) throws IOException {
    // Get a Path object
    Path path = Paths.get("C:\\myData\\Main.java");

    // Prepare the attribute list
    String attribList = "basic:size,lastModifiedTime";

    // Read the attributes
    Map<String, Object> attribs = Files.readAttributes(path, attribList);

    // Display the attributes on the standard output
    System.out.format("Size:%s, Last Modified Time:%s %n", attribs.get("size"),
        attribs.get("lastModifiedTime"));
  }/*from  w w w .j  ava 2  s .com*/
}

Result

Related Topic