集册 Java实例教程 使用readAttributes()获取大容量属性

使用readAttributes()获取大容量属性

欢马劈雪     最近更新时间:2020-01-02 10:19:05

779
您可以使用readAttributes()方法批量提取属性。

import java.io.IOException;

import java.nio.file.Files;
/**
N o w  J a v a  .   c o m
**/

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.attribute.BasicFileAttributes;


public class Main {

  public static void main(String[] args) {


    BasicFileAttributes attr = null;

    Path path = Paths.get("D:\\folder0\\Java\\JDK7\\folder5\\code\\folder1\\folder2\\folder6", "test.txt");


    //extract attributes as bulk with readAttributes

    try {

        attr = Files.readAttributes(path, BasicFileAttributes.class);

    } catch (IOException e) {

        System.err.println(e);

    }


    System.out.println("File size: " + attr.size());

    System.out.println("File creation time: " + attr.creationTime());
    /* 
     来自 
    *nowjava*/

    System.out.println("File was last time accessed at: " + attr.lastAccessTime());

    System.out.println("File was last time modified at: " + attr.lastModifiedTime());


    System.out.println("Is directory ? " + attr.isDirectory())
展开阅读全文