import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.LineNumberInputStream;
import java.io.IOException;
class lcat {
public static void main (String args[]) {
String thisLine;
//Loop across the arguments
for (int i=0; i < args.length; i++) {
//Open the file for reading
try {
FileInputStream fin = new FileInputStream(args[i]);
// chain the DataInputStream to a LineNumberInputStream
LineNumberInputStream lnis = new
LineNumberInputStream(fin);
// now turn the FileInputStream into a DataInputStream
try {
DataInputStream myInput = new DataInputStream(lnis);
try {
while ((thisLine = myInput.readLine()) != null) {
System.out.println(lnis.getLineNumber() + ": " +
thisLine);
} // while loop ends here
}
catch (Exception e) {
System.err.println("Error: " + e);
}
} // end try
catch (Exception e) {
System.err.println("Error: " + e);
}
} // end try
catch (Exception e) {
System.err.println("failed to open file " + args[i]);
System.err.println("Error: " + e);
}
} // for ends here
} // main ends here
}
Here's the first few lines produced when this program is run on itself.
% java lcat lcat.java 1: import java.io.FileInputStream; 2: import java.io.DataInputStream; 3: import java.io.LineNumberInputStream; 4: 5: 6: class lcat { 7: 8: public static void main (String args[]) { 9: 10: String thisLine; 11: 12: //Loop across the arguments 13: for (int i=0; i < args.length; i++) { 14: 15: //Open the file for reading 16: try {