Java Files get file attribute map for view name

Introduction

The syntax for the String argument consists of an optional viewName.

A viewName is typically one of the following:

  • acl
  • basic
  • owner
  • user
  • dos
  • posix

The following table illustrates how to use basic view:

String Attributes returned
"*"All of the basic file attributes
"basic:*" All of the basic file attributes
"basic:isDirectory,lastAccessTime" Only the isDirectory and lastAccessTime attributes
"isDirectory,lastAccessTime"Only the isDirectory and lastAccessTime attributes

There cannot be any embedded spaces in the attribute String.

For example, the String, "basic:isDirectory, lastAccessTime", where there is a blank after the comma will result in an IllegalArgumentException.

import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Set;

public class Main {

   public static void main(String[] args) throws Exception {
      Path path = Paths.get("Main.java");

      Map<String, Object> attrsMap = Files.readAttributes(path, "dos:*", LinkOption.NOFOLLOW_LINKS); 
      Set<String> keys = attrsMap.keySet();

      for (String attribute : keys) {
         System.out.println(attribute + ": " + Files.getAttribute(path, attribute));
      }//from  w  w w. j  a  v a2  s.c o m

   }

}



PreviousNext

Related