Start Search Contents Index Links About
dir.java
/*
 * Author: Havard Rast Blok
 * E-mail: 
 * Web   : www.rememberjava.com
 */

import java.io.File;
import java.text.DateFormat;
import java.util.Date;

/**
 * Lists the files in the current directory. One file per line.
 * Similuar to "dir" in DOS-prompt
 */
public class dir
{

  public dir()
  {
    String homeDir = "";
    File file;
    File fileList[];
    String fileName;
    String fileDate;
    long fileLength;
    boolean isDir;
    boolean isHidden;
    
    DateFormat df;
    
    //Get the current directory
    try
    {
      homeDir = System.getProperty"user.dir" );  
    }
    catch(Exception e)
    {
      System.out.println("dir.dir - Unable to retrieve current directory: "+e);
      System.exit(1);
    }
    
    //Get file names of the current directory
    file = new FilehomeDir );
    fileList = file.listFiles();
    iffileList == null )
    {
      System.out.println("dir.dir - Error accessing current directory.");
      System.exit(1);
    }
    
    //Set the date format
    df = DateFormat.getDateTimeInstanceDateFormat.SHORT, DateFormat.SHORT );
    
    //List the files in the current directory. One file per line.
    for(int i = 0; i < fileList.length; i++)
    {
      //check if file is hidden...
      isHidden = fileList[i].isHidden();
      
      //...and show only file which are not
      if!isHidden )
      {
        fileName = fileList[i].getName();
        fileDate = df.formatnew DatefileList[i].lastModified() ) );
        fileLength = fileList[i].length();
        isDir = fileList[i].isDirectory();
        
        //Notice that the tabulation with large files is not very good.
        System.out.println(fileDate + "\t" (isDir?"<DIR>\t":"     \t"+fileLength"\t" + fileName);
      }
    }
    
  
  public static void mainString [] args)
  {
    new dir();
  }

}


site: Håvard Rast Blok
mail:
updated: 27 July 2007