Get file attribute - Java File Path IO

Java examples for File Path IO:File Attribute

Description

Get file attribute

Demo Code


import java.io.IOException;
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);
        }//w  w w.  j a  v  a 2  s.  com
        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
                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': " + attrs.creationTime() + " - " + attrsDirect.creationTime());
        System.out.println("View attribute 'isRegularFile': " + attrs.isRegularFile() + " - " + attrsDirect.isRegularFile());
        System.out.println("View attribute 'isSymbolicLink': " + attrs.isSymbolicLink() + " - " + attrsDirect.isSymbolicLink());
    }
}

Related Tutorials