Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Program to demonstrate how to display File information in Java ?.

Introduction
In this section of blog, we will look into a simple program that will demonstrate how to display File information in Java. The methods used here are defined in File class present in java.io.* package. For more details of this method read my blog post : Creating objects and methods list of File class in Java

package com.hubberspot.file.example;

import java.io.*;

/**
 *
 * @author Jontymagicman
 */

public class FileInformation {
  public static void main(String[] args){
        
    File file = new File("E:\\notepad.txt");
        
    System.out.println("File Name : "+ file.getName()); 
 
 System.out.println("File last modified : "+ file.lastModified()); 
 
 System.out.println("File size : " + file.length() + " Bytes"); 
 
 System.out.println("Path : "+file.getPath()); 
 
 System.out.println("Abs Path : "+file.getAbsolutePath()); 
 
 System.out.println("Parent : "+ file.getParent()); 
 
    System.out.println(file.exists() ? +
+"File exists":"File does not exist"); 
 
    System.out.println(file.canWrite() ? +
+"File is writable" : "File is not writable"); 
 
 System.out.println(file.canRead() ? +
+"File is readable" : "File is not readable"); 
 
    System.out.println(file.isHidden() ? +
+"File is hidden" : "File is not hidden"); 
 
 System.out.println(file.isDirectory() ? +
+"Is a directory" : "Is not a directory"); 
 
    System.out.println(file.isFile() ? +
+"Is a file" : "Is not a file"); 
 
    System.out.println(file.isAbsolute() ? +
+"File is absolute" : "File is not absolute" );
        
  }
    
}

Output of the program




 
© 2021 Learn Java by Examples Template by Hubberspot