Posted By:
David_Bates
Posted On:
Sunday, August 10, 2003 02:22 AM
Huh?
OK. I understand the command "cat *.java" in unix/linux. This will display the contents of all the Java files in the current directory. Fine.
Next, you start talking about some Swing application that I've never heard about. All you explain is the user interface and how the user selects the command, nothing about how the user request is actually executed!
Please be more specific!
If I was to write "cat" in Java, it'd look something like this:
import java.io.*;
public class Cat {
public static void main(String[] args) throws IOException {
// Loop through each file...
for (int i = 0; i < args.length; i++) {
display(args[i]);
}
}
public static void display(String fileName) throws IOException {
File f = new File(fileName);
// Check the file actually exists...
if (!f.exists()) {
System.out.println("File '" + fileName + "' does not exist.");
return;
}
// Check the file isn't a directory...
else if (f.isDirectory()) {
System.out.println("'" + fileName + "' is a directory and cannot be displayed.");
return;
}
// Read and display the contents of the file.
BufferedReader br = new BufferedReader(new FileReader(f));
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
}
}