集册 Java实例教程 获取文件属性

获取文件属性

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

544
获取文件属性


import java.io.IOException;// 来 自 时代Java公众号

import java.nio.file.*;

import java.nio.file.attribute.BasicFileAttributeView;

import java.nio.file.attribute.BasicFileAttributes;

import java.nio.file.attribute.DosFileAttributeView;


public class FileAttributes {


    public static void main(String[] args) throws IOException {

        final FileSystem defaultFS = FileSystems.getDefault();

        for (String fileAttributeView : defaultFS.supportedFileAttributeViews()) {

            System.out.println("Default file system supports: " + fileAttributeView);

        }

        System.out.println();


        final Path zipPath = Paths.get("/tmp/a2j_in.zip");

        try (FileSystem zipFS = FileSystems.newFileSystem(zipPath, null)) {

            for (FileStore fs : zipFS.getFileStores()) {

                // returns true

                fs.supportsFileAttributeView(BasicFileAttributeView.class);

                // returns false
                /**
                n o w j a v a . c o m - 时代Java 提供 
                **/

                fs.supportsFileAttributeView(DosFileAttributeView.class);

                // returns false

                fs.supportsFileAttributeView("acl");

            }

        }


        final Path path = Paths.get("/tmp/a2j_in.zip");

        final BasicFileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class);

        final BasicFileAttributes attrs = view.readAttributes();

        final BasicFileAttributes attrsDirect = Files.readAttributes(path, BasicFileAttributes.class);

        System.out.println("View attribute 'creationTime': "
展开阅读全文