Get the metadata of a file

后端 未结 5 2034
难免孤独
难免孤独 2020-11-27 20:11

It is possible to know the metadata of a file in java? and if it is, How to get the metadata of a file in java?

5条回答
  •  失恋的感觉
    2020-11-27 20:42

    There is a basic set of metadata that you can get from a file.

    Path file = ...;
    BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
    
    System.out.println("creationTime: " + attr.creationTime());
    System.out.println("lastAccessTime: " + attr.lastAccessTime());
    System.out.println("lastModifiedTime: " + attr.lastModifiedTime());
    
    System.out.println("isDirectory: " + attr.isDirectory());
    System.out.println("isOther: " + attr.isOther());
    System.out.println("isRegularFile: " + attr.isRegularFile());
    System.out.println("isSymbolicLink: " + attr.isSymbolicLink());
    System.out.println("size: " + attr.size());
    

    Some things are platform dependent and may throw exceptions or return unexpected results.

    You can read more at Managing Metadata (File and File Store Attributes).

提交回复
热议问题