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

import java.io.File;

/**
 * Lists the files in the current directory. One file per line.
 * Similuar to "dir /b" in DOS-prompt of Windows or
 * "ls -1" in Linux
 */
public class ls
{

  public ls()
  {
    String homeDir = "";
    File file;
    String fileList[];

    //Get the current directory
    try
    {
      homeDir = System.getProperty"user.dir" );  
    }
    catch(Exception e)
    {
      System.out.println("ls.ls - Unable to retrieve current directory: "+e);
      System.exit(1);
    }
    
    //Get file names of the current directory
    file = new FilehomeDir );
    fileList = file.list();
    iffileList == null )
    {
      System.out.println("ls.ls - Error accessing current directory.");
      System.exit(1);
    }
    
    //List the files in the current directory. One file per line.
    for(int i = 0; i < fileList.length; i++)
    {
      System.out.println(fileList[i]);
    }


    
  }
  
  public static void mainString [] args)
  {
    new ls();
  }

}


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