Java IO & NIO - Files.getFileAttributeView() Examples

Code Snippets Java IO & NIO Java 


Class:

java.nio.file.Files

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

Method:

public static <V extends FileAttributeView> V getFileAttributeView(Path path,
                                                                   Class<V> type,
                                                                   LinkOption... options)

Returns a file attribute view of a given type. A file attribute view provides a read-only or updatable view of a set of file attributes.

Type Parameters:
V - The FileAttributeView type
Parameters:
path - the path to the file
type - the Class object corresponding to the file attribute view
options - options indicating how symbolic links are handled
Returns:
a file attribute view of the specified type, or null if the attribute view type is not available


Examples


package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.concurrent.TimeUnit;

public class GetFileAttributeViewExample {

public static void main(String... args) throws IOException {
Path tempFile = Files.createTempFile("test-file", ".txt");
BasicFileAttributeView fileAttributeView =
Files.getFileAttributeView(tempFile, BasicFileAttributeView.class);
System.out.println(fileAttributeView);
//get basic attributes
BasicFileAttributes basicFileAttributes = fileAttributeView.readAttributes();
System.out.println(basicFileAttributes.creationTime());
//modify times
FileTime from = FileTime.from(400, TimeUnit.HOURS);
fileAttributeView.setTimes(from, from, from);
//read last modify
FileTime lastModifiedTime = Files.getLastModifiedTime(tempFile);
System.out.println(lastModifiedTime);

}
}

Output

sun.nio.fs.WindowsFileAttributeViews$Basic@30b746c0
2017-12-11T03:47:23.681553Z
1970-01-17T16:00:00Z




See Also