Java IO & NIO - Files.readAttributes() Examples

Code Snippets Java IO & NIO Java 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Methods:

public static <A extends BasicFileAttributes> A readAttributes(Path path,
                                                               Class<A> type,
                                                               LinkOption... options)
                                                        throws IOException

Reads a file's attributes.

Type Parameters:
A - The BasicFileAttributes type
Parameters:
path - the path to the file
type - the Class of the file attributes required to read
options - options indicating how symbolic links are handled
Returns:
the file attributes


public static Map<String,Object> readAttributes(Path path,
                                                String attributes,
                                                LinkOption... options)
                                         throws IOException
Parameters:
path - the path to the file
attributes - the attributes to read
options - options indicating how symbolic links are handled
Returns:
a map of the attributes returned; The map's keys are the attribute names, its values are the attribute values


Examples


package com.logicbig.example.files;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.DosFileAttributes;
import java.nio.file.attribute.PosixFileAttributes;

public class ReadAttributesExample {

public static void main(String... args) throws Exception {
Path path = Files.createTempFile("test-file", ".txt");
try {
BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
printBasicAttributes(attributes);
} catch (Exception e) {
}
try {
//it will throw not supported exception on windows
PosixFileAttributes attributes = Files.readAttributes(path, PosixFileAttributes.class);
//printPosixFileAttributes(attributes)

} catch (Exception e) {
}
try {
DosFileAttributes attributes = Files.readAttributes(path, DosFileAttributes.class);
printDosAttributes(attributes);
} catch (Exception e) {
}
}

private static void printBasicAttributes(BasicFileAttributes attributes) throws Exception {
System.out.println("-- Some BasicFileAttributes --");
System.out.printf("creationTime = %s%n", attributes.creationTime());
System.out.printf("lastAccessTime = %s%n", attributes.lastAccessTime());
System.out.printf("lastModifiedTime = %s%n", attributes.lastModifiedTime());
System.out.printf("size = %s%n", attributes.size());
System.out.printf("directory = %s%n", attributes.isDirectory());

}

private static void printDosAttributes(DosFileAttributes attributes) {
System.out.println("-- Some DosFileAttributes --");
System.out.printf("archive = %s%n", attributes.isArchive());
System.out.printf("readOnly = %s%n", attributes.isReadOnly());
System.out.printf("hidden = %s%n", attributes.isHidden());
}
}

Output

-- Some BasicFileAttributes --
creationTime = 2017-12-17T17:31:18.114827Z
lastAccessTime = 2017-12-17T17:31:18.114827Z
lastModifiedTime = 2017-12-17T17:31:18.114827Z
size = 0
directory = false
-- Some DosFileAttributes --
archive = true
readOnly = false
hidden = false




package com.logicbig.example.files;

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

public class ReadAttributesExample2 {

public static void main(String... args) throws IOException {
Path path = Files.createTempFile("test-file", ".txt");

System.out.println("-- Some Basic Attributes --");
//same as "basic:lastModifiedTime....", default attribute type is basic
Map<String, Object> attributes =
Files.readAttributes(path, "lastModifiedTime,lastAccessTime");
attributes.entrySet().forEach(entry ->
System.out.printf("%18s = %s%n", entry.getKey(), entry.getValue()));

System.out.println("-- all Basic Attributes --");
attributes =
Files.readAttributes(path, "*");
attributes.entrySet().forEach(entry ->
System.out.printf("%18s = %s%n", entry.getKey(), entry.getValue()));

System.out.println("-- All Dos Attributes --");
attributes = Files.readAttributes(path, "dos:*");
attributes.entrySet().forEach(entry ->
System.out.printf("%15s = %s%n", entry.getKey(), entry.getValue()));
}
}

Output

-- Some Basic Attributes --
lastAccessTime = 2017-12-17T17:31:20.735133Z
lastModifiedTime = 2017-12-17T17:31:20.735133Z
-- all Basic Attributes --
lastAccessTime = 2017-12-17T17:31:20.735133Z
lastModifiedTime = 2017-12-17T17:31:20.735133Z
size = 0
creationTime = 2017-12-17T17:31:20.735133Z
isSymbolicLink = false
isRegularFile = true
fileKey = null
isOther = false
isDirectory = false
-- All Dos Attributes --
lastAccessTime = 2017-12-17T17:31:20.735133Z
lastModifiedTime = 2017-12-17T17:31:20.735133Z
creationTime = 2017-12-17T17:31:20.735133Z
hidden = false
isRegularFile = true
fileKey = null
archive = true
system = false
size = 0
readonly = false
isSymbolicLink = false
attributes = 32
isOther = false
isDirectory = false




package com.logicbig.example.files;

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

public class ReadAttributesExample3 {

public static void main(String... args) throws IOException {
Path dirPath = Files.createTempDirectory("test-dir");
Path filePath = Files.createTempFile(dirPath, "test-file", ".txt");
System.out.println("-- src attributes --");
printAllDosAttribute(filePath);
//must run as admin
Path symPath = Files.createSymbolicLink(dirPath.resolve("sym-tst-file.txt"), filePath);
System.out.println("-- sym attributes --");
printAllDosAttribute(symPath);

System.out.println("-- sym attributes with NOFOLLOW_LINKS --");
printAllDosAttribute(symPath, LinkOption.NOFOLLOW_LINKS);

}

private static void printAllDosAttribute(Path filePath, LinkOption... options) throws IOException {
Files.readAttributes(filePath, "dos:*", options)
.entrySet()
.forEach(ReadAttributesExample3::printAttributes);
}

private static void printAttributes(Map.Entry<String, Object> entry) {
System.out.printf("%18s = %s%n", entry.getKey(), entry.getValue());
}
}

Output

-- src attributes --
lastAccessTime = 2017-12-17T17:51:48.45153Z
lastModifiedTime = 2017-12-17T17:51:48.45153Z
creationTime = 2017-12-17T17:51:48.45153Z
hidden = false
isRegularFile = true
fileKey = null
archive = true
system = false
size = 0
readonly = false
isSymbolicLink = false
attributes = 32
isOther = false
isDirectory = false
-- sym attributes --
lastAccessTime = 2017-12-17T17:51:48.45153Z
lastModifiedTime = 2017-12-17T17:51:48.45153Z
creationTime = 2017-12-17T17:51:48.45153Z
hidden = false
isRegularFile = true
fileKey = null
archive = true
system = false
size = 0
readonly = false
isSymbolicLink = false
attributes = 32
isOther = false
isDirectory = false
-- sym attributes with NOFOLLOW_LINKS --
lastAccessTime = 2017-12-17T17:51:48.461537Z
lastModifiedTime = 2017-12-17T17:51:48.461537Z
creationTime = 2017-12-17T17:51:48.461537Z
hidden = false
isRegularFile = false
fileKey = null
archive = true
system = false
size = 0
readonly = false
isSymbolicLink = true
attributes = 1056
isOther = false
isDirectory = false




See Also