Java Reference
In-Depth Information
// Raad 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"));
Listing 10-14 reads the basic file attributes of the file C:\poems\luci1.txt and prints some of them on
the standard output. You will need to change the file path in the main() method to work with another file on
your platform. You may get a different output when you run this program. If the specified file does not exist, a
NoSuchFileException is thrown and the program prints the stack trace of the exception.
Listing 10-14. Reading the Basic File Attributes of a File
// BasicFileAttributesTest.java
package com.jdojo.nio2;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
public class BasicFileAttributesTest {
public static void main(String[] args) {
// Change the file path of an existing file
Path path = Paths.get("C:\\poems\\luci1.txt");
try {
// Read basic file attributes
BasicFileAttributes bfa =
Files.readAttributes(path, BasicFileAttributes.class);
// Print some of the basic file attributes
System.out.format("Size:%s bytes %n", bfa.size());
System.out.format("Creation Time:%s %n",
bfa.creationTime());
System.out.format("Last Access Time:%s %n",
bfa.lastAccessTime());
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Size:119 bytes
Creation Time:2014-05-05T20:52:16.589994Z
Last Access Time:2014-05-05T20:52:16.589994Z
 
Search WWH ::




Custom Search