Viewing file properties

The File object is a representation of a file or a directory on disk. The File object can be used to extract properties of a file apart from other functions of renaming, deletion etc..


The following code is pretty self-explanatory. The program asks for a file name and displays the properties of the file/directory that is entered. It explains how a File object can be used to extract details of a File or Directory. 

 * This example lists out the properties of a File and demonstrates 
 * how a File Object can be used. 
 * Comments about individual functions are interspersed. 
 * @author Anand Hariharan ( 

public class FileExample {
    public static void main(String[] args) {
        // Get the File Name from the user 
        System.out.print("Enter the full path of the file you want to list properties for..");
        String fileName = null;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(;
            fileName = br.readLine();
        } catch (IOException ioe) {
            System.out.println("Couldnt read filename, some problem");

        // Create a File Object with the entered name. 
        // NOTE : Creating a File object in Java will *NOT* create 
        // the corresponding file on disk. The File object can be used 
        // extract properties about an existing file. If the file does 
        // not exist on disk, the File Object will be created nevertheless. 

        File file = new File(fileName);
        // Check if this file exists 
        if (!file.exists()) {
            // File does not exist, give error and exit 
            System.out.println("The File \"" + fileName + "\" " +
                    "does not exist. Please enter a valid FileName.");

        System.out.println("**Properties of File " + file.getAbsolutePath() + "**");

        //First check if the File Object is a Directory. 
        //NOTE : The File object can be used to represent directories also. 
        if (file.isDirectory())
            System.out.println(file.getPath() + " is a directory.");
        else {
            // Size of the file in bytes... 
            System.out.println("Size of file in bytes... " + file.length());

        // Check permissions to read and write. 
        if (file.canWrite()) {
            if (file.canRead())
                System.out.println(file.getPath() + " is read-write.");
                System.out.println(file.getPath() +
                        " cannot be read from, but write permissions are allowed.");
        } else {
            if (file.canRead())
                System.out.println(file.getPath() + " is read-only");
                System.out.println("You cannot read or write to " +

        // Check the parent of this file.. 
        String parent = file.getParent();
        if (parent == null) {
            System.out.println(file.getPath() + " is a root directory.");
        } else {
            System.out.println("Parent of " + file.getPath() + " is " + parent + ".");
        // Check if file is hidden. 
        if (file.isHidden()) {
            System.out.println(file.getPath() + " is Hidden.");

        // Check when it was last modified.. 
        System.out.println(file.getPath() + " was last modified on " +
                new java.util.Date(file.lastModified()));


Leave a Reply