Start Search Contents Index Links About

ls and dir

File operations and enviornment variables

2001 - Week 47 - Havard Rast Blok

In this first week of the Java exercises I will look at the ls command of Linux and dir command of Windows. You will implement your own version of these commands.

Yours first goal is to imitate the 'dir /b' from the DOS-prompt of Windows or 'ls -1' command from Linux. (Try these out, if you are not sure how they work.) The files of the current directory should be listed, and only the names of the files should be shown, one file per line. Your application do not need any input arguments. Its only function is to list all the files of the current directory.

Hint: System.getProperty(...) and File.list()


ls.java

You have now listed the names of the files in the current directory, and will not enhance the functionality of your application. The default output of the dir and ls is, in addition to the name of the files, their size and last modified date. In Linux, the owner and group are also listed. Your task is now to imitate the command of your favourite OS.

Please be aware the owner and group of a file is not included in the java.io.File class. Also notice that the column and row layout of both dir and ls might be hard to implement. Do not spend too much time on that task.


dir.java

The final small task of this week is to combine the two above. Your application should be able to output the bare format of exercise 1 and the full information shown in 2, thus you will type java ls or java ls 1 to choose format. To achieve this you need to implement parameter checking.

Hint: The parameters after ls on the command line are passed to the main method of your class. Thus you need to compare the String array of main to the allowed option(s) of your application.


dir.java

If you feel ambitious this week, you could further enhance your application by adding more parameters. Please refer to the dir /? or man ls for information on legal options. You could also add the filter feature, by allowing only selected files to be shown. This would allow a syntax like java ls ls.class or java ls *.java. Refer to the Java API for which methods of File to use.



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