File Attributes

This application illustrates most of the methods included in the File class for accessing file attributes, such as length, date of creation, etc.

import java.awt.*;
import java.io.*;

public class FileDemo  {
//  show file attributes

  public FileDemo ()  {
    super ();
  }  // end FileDemo constructor

  public static void main ( String [ ] args )  {
  String fileName;
  String path;
  File file;
  String [] directory;

  FileDemo fileDemo = new FileDemo ();

  if ( args.length > 0 ) fileName= args[0];
  else {
    fileName = new String ( "testfile" );
    path = new String ( "../" );
  }
  if ( args.length > 1 ) path = args[1];
  else path = new String ( "../" );

  file = new File ( path, fileName );

    if ( ! file.exists () )  {
      System.out.println ( file.getName() + " does not exist");
    }  // end if
    else {
      System.out.println ( file.getName() + "  exists:");
      if ( file.isFile () )
        System.out.println ( "  file");
      if ( file.isDirectory () )
        System.out.println ( "  directory");
      System.out.println ( "  length = " + file.length () );
      System.out.println ( "  last modified = " + file.lastModified () );
      System.out.println ( "  path = " + file.getPath () );

      if ( file.isDirectory () ) {
        System.out.println ( "Directory contents:");
        directory =  file.list();
        for ( int i=0; i < directory.length; i++ )
          System.out.println ( "  " + directory[ i ] );
      }  // end for

    }  // end else

  }  // end main

}  // end FileDemo