Java – get the metadata of the file

Can I know the metadata of a file through Java? If so, how to get the metadata of the file in Java?

Solution

You can get a basic set of metadata from the 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 I throw exceptions or return unexpected results

You can read more in managing metadata (file and file store attributes)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>