Netprog-Java
Spring 2001

Homework 2
Due Date: Tuesday, Jan 30th (by 11:59PM)

Submit to javanetprog-submit@cs.rpi.edu with the subject line "2"
Complete Submission instructions are here


This homework involves writing 2 programs, the first is described below (the head command). The second part of the assignment is not yet complete... (will be later today).

Part 1: Java I/O

This part of the homework involves the development of a program similar to the Unix head command. The head command reads a text file and prints out (sends to stdout) the first few lines of the file. The name of the file to be processed is provided as a command line argument, and optionally the number of lines to be displayed can be specified. Included below is the man page for head from BSD:


HEAD(1)                 FreeBSD General Commands Manual                HEAD(1)

NAME
     head - display first lines of a file

SYNOPSIS
     head [-n count] [-c bytes] [file ...]

DESCRIPTION
     This filter displays the first count lines or bytes of each of the speci-
     fied files, or of the standard input if no files are specified.  If count
     is omitted it defaults to 10.

     If more than a single file is specified, each file is preceded by a head-
     er consisting of the string ``==> XXX <=='' where ``XXX'' is the name of
     the file.

     The head utility exits 0 on success, and >0 if an error occurs.

SEE ALSO
     tail(1)

Your head program should support all the command line options described in the above man page!


Part 2: Threads

This part of the homework involves writing a program that can act like the Unix command tail -f. The Unix tail command is similar to the head command, but it usually shows just the last few lines of a file. However, when the -f option is used on the command line the tail command will not stop reading when an end of file condition exists. tail -f will continue looking for additional data to be appended to the file, and will send this data to stdout as it appears in the file. The result is a useful utility if you are trying to watch a server log file, you do something like this:

> tail -f http.log

and the output will show the last few lines of the file http.log and will then display anything new that is appended to the file http.log. Try this on any CS workstation and you will see what is happening (while this is running, load a page from www.cs.rpi.edu and you should see the log entry for that page appear in the log file):

> tail -f /usr/local/www/logs/www.cs.rpi.edu/access_log

To quit the program, press Ctrl-C

.

You are to write a Java program that does the same thing as tail -f - it should print out the last few lines of the file specified on the command line, and should then watch the file for additional data and print any new data out. In addition, your program should quit whenver the user types a 'q or 'Q' (your program should look for this input from stdin). Your program should have multiple threads, one that handles watching the file for new data, and one that watches stdin.

Testing: To test your program, you need to be able to append to an existing file (so that your program will have something new to display). You can use the shell >> redirection operator to do this, even in a DOS window. For example, the following command would add 4 new lines (the first 4 lines of the file head.java) to the file blah, this assumes you have a program named head from the first part of this assignment:

java head -4 head.java >> blah

To test your program you should run it on the file blah (in a different window/shell), and then use the above command to add new stuff to the file blah.


Submitting your files

Submit your programs by emailing all the source files to javanetprog-submit@cs.rpi.edu (with the subject line set to '2') along with a README file that briefly describes your files.