Read a File Meta Data Using Java

Category: Java   Tags: Java, Java File Handling, Java Path, Java NIO File System

To read the basic attributes of a file, you can use one of the Files.readAttributes methods. Here is one Java program for this:

JavaFileMetaDataBasicAttrExample.java

                                package com.tutorial.java.files;

                                import java.io.IOException;
                                import java.nio.file.Files;
                                import java.nio.file.Path;
                                import java.nio.file.Paths;
                                import java.nio.file.attribute.BasicFileAttributes;

                                public class JavaFileMetaDataBasicAttrExample {

                                    public static void main(String[] args) throws IOException {
                                        Path file = Paths.get("sample.txt");
                                        BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
                                        System.out.println("Creation Time: " + attr.creationTime());
                                        System.out.println("Is Directory: " + attr.isDirectory());
                                        System.out.println("Last Access Time: " + attr.lastAccessTime());
                                        System.out.println("Last Modified Time: " + attr.lastModifiedTime());
                                        System.out.println("Is Regular File: " + attr.isRegularFile());
                                        System.out.println("Is Symbolic Link: " + attr.isSymbolicLink());
                                    }
                                }
                            

Output:

                            Creation Time: 2017-10-26T15:25:07Z
                            Is Directory: false
                            Last Access Time: 2017-10-28T09:24:36Z
                            Last Modified Time: 2017-10-26T15:25:07Z
                            Is Regular File: true
                            Is Symbolic Link: false
                          

DOS File Attributes Using Java

To read the DOS attributes of a file, you can use one of the DosFileAttributes methods. Here is one Java program for this:

JavaFileMetaDataDOSAttrExample.java

                                package com.tutorial.java.files;

                                import java.io.IOException;
                                import java.nio.file.Files;
                                import java.nio.file.Path;
                                import java.nio.file.Paths;
                                import java.nio.file.attribute.DosFileAttributes;

                                public class JavaFileMetaDataDOSAttrExample {

                                    public static void main(String[] args) throws IOException {
                                        Path file = Paths.get("sample.txt");
                                        DosFileAttributes attr = Files.readAttributes(file, DosFileAttributes.class);

                                        System.out.println("Creation Time: " + attr.creationTime());
                                        System.out.println("Is Directory: " + attr.isDirectory());
                                        System.out.println("Last Access Time: " + attr.lastAccessTime());
                                        System.out.println("Last Modified Time: " + attr.lastModifiedTime());
                                        System.out.println("Is Regular File: " + attr.isRegularFile());
                                        System.out.println("Is Symbolic Link: " + attr.isSymbolicLink());
                                    }
                                }
                            

Output:

                            Creation Time: 2017-10-28T10:25:36Z
                            Is Directory: false
                            Last Access Time: 2017-10-28T10:25:37Z
                            Last Modified Time: 2017-10-28T10:25:36Z
                            Is Regular File: true
                            Is Symbolic Link: false