Some FileIO Practice
You will write three programs that will take files and display
a portion of them to the screen. Feel free to frankencode from
Grip.java
as it has the basic template for reading a file
line by line.
Here is a test file, test.dat
you can run these on.
Feel free to add lines.
533 221 443 221 444 321 425 222 889 cow pig horse
Head.java
this takes two command-line arguments
and you run it like this.
unix> java Head 2 test.dat 533 221 443 221 444 321 425 222
This will print out the first (number) lines of file (filename).
Tail.java
this takes two command-line arguments. You
run it like this.
unix> java Tail 2 test.dat 444 321 425 222 889 cow pig horse
This will print out the last (number) lines of code in the file.
Squawk.java
This program extracts columns from a file
with columnar data. Do not worry about checking on the numbrer of
columns in the file.
java Squawk filename num1, num2, num3....
This takes as arguments a filename (filename) and a sequence of numerical arguments. The file is assumed to be a columnar file of date looking like this
unix> java test.dat 1 3 533 433 444 425 889 pig
Here are some hints.
- read the file line by line
- For
Tail.java
, you might want to store the lines in an array list. - For
Squawk.java
, split each line and use the indices in the resulting arrays to print out the entry for each column. To split a string use.split("\\s+)
. Make an integer array and useInteger.parseInt
to convert the command-line arguments after ther file name into integers to populate the array. Then the printing of lines is easy.