Way2Java

File class Retrieve Metadata of File

Almost all the java.io package classes are streams. A stream is a carrier of data. To carry data, even sockets, use I/O streams. Infact, the methods getInputStream() and getOutputStream() of java.net.Socket returns an object of InputStream class and OutputStream class.

InputStreamReader and OutputStreamWriter are non-stream classes. They are not streams; they are converters or wrappers between input streams and output streams.

One more non-stream class in java.io package is File. Using this class, the properties of a file can be obtained like the file has got write permission or when the file is created etc.

Two programs are given using File class.

  1. File class – Retrieving Metadata of FileRetrieving File Metada
  2. File class – Changing File Properties

Retrieving File Metadata

The following program retrieves file metadata using different methods of File class.

import java.io.*;
public class FileMetaData   
{
  public static void main(String args[ ] ) throws IOException 
  {                                                           		
    System.out.println("pathSeparatorChar = " + File.pathSeparatorChar); 
    System.out.println("separatorChar = " + File.separatorChar);     
			                   
    File file1 = new File("pqr.txt"); 
    System.out.println("getName(): " + file1.getName());		
			
    System.out.println("getAbsolutePath(): " + file1.getAbsolutePath()) ;		
    System.out.println("canRead(): " + file1.canRead()) ;
    System.out.println("canWrite(): " + file1.canWrite()) ;
    System.out.println("isFile(): " + file1.isFile()) ;		
    System.out.println("isDirectory: " + file1.isDirectory()) ;		
    System.out.println("File last modified date: " + file1.lastModified()); 
    System.out.println("File size: " + file1.length() + " Bytes");       
    System.out.println("File exists(): " + file1.exists());		
  }   
}


Output screenshot of File class Retrieve Metadata of File

File.pathSeparatorChar;
File.separatorChar;

The first statement prints "; (semicolon)" and the next prints "\ (backward slash)". They are used in setting the environment variables – classpath and path. They are operating system dependent. When a Java program is being executed, the programmer can obtain them by using the above static variables (not methods) of File class.

File file1 = new File("pqr.txt");

The file name whose metadata is to be obtained is passed as parameter to the File constructor. Infact, the File object file1 can be passed to FileInputStream and FileOutputStream constructors instead of direct file name.

All the methods of File class are self-explanatory.

getName() : Returns the name of the file
getPath() : Returns the classpath of the file
canRead() : Returns true the if the file can be read
canWrite() : Returns true if the file can be written
isFile() : Returns true if the parameter passed is a file
isDirectory() : Returns true if the parameter passed is a directory
lastModified() : Returns the file creation/latest modified time (which ever is latest) in seconds from epoch time (refer Calendar class)
length() : Returns the file in bytes
exists() : Returns true if the file exists in the current directory