Tagbangers Blog

Replicating the "cat" Command with Java

Hi, it's Yu again. Not you, it's me, Yu. I know right. It's such a confusing name.

In the last article, I did a brief recap on the first month as an Intern.

I've created a list of things I've done, but that was it. There wasn't much explanation on how and why I did those things, and the problems I've faced and how I solved them. And I do not think that is good. 

I mean, one of the biggest purposes of writing these articles is to keep track of what I've learned. But if I list just the things I've done and leave out all the processes of it, that will not tell what I've learned. The only way I can tell what I've learned, I believe, is to show how I did it and why I did it.

Thus, I'm planning to write an article for each of the things I have done so far, and in those articles I will be focusing on the hows and the whys of the things I've done. That way, I think I would be able to better retain what I've learned, and when I look back I would be able to explain every single line of code. 

For this article, as a start, I'd like to explain how I wrote the "cat" command using Java.


The source code can be seen here.

Separating the Operations

The first thing I did was to separate the operations with two different conditions possible: cat command with an argument, and without an argument.

I did this by using the if-else statement.

if (args.length != 0) {        // with an argument
} 
else {        // without an argument
}

How does this work? Well, it's pretty simple. Let's have a closer look at the condition of the if statement.

args.length != 0

"args.length" is the length of the command-line arguments. The "!=" operator means "is not equal to". So the condition is "if the length of the command-line arguments is not equal to zero", which is in other words "when there is an argument". Easy peasy right?

Now that we know what the condition of the if statement is for, we also know what "else" is for. Just in case, it is for "when there is no argument".

Coding the Actual Operations

So the separation of operations was complete. The next thing I did was coding the actual operations. 

Here's the code for cat command with an argument. The comments on the end of each line explains everything.

try {
    FileReader r = new FileReader(file);        //reads file
    BufferedReader br = new BufferedReader(r);      //buffers file to speed up operation.
    String line;
    while ((line = br.readLine()) != null) {        //prints each line inside of a file until there is no line left.
        System.out.println(line);
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();        // displays error details
} catch (IOException e) {
    e.printStackTrace();        // displays error details
}

Straight and simple. Nothing complex.

Here's the code for cat command without an argument:

Scanner sc = new Scanner(System.in);        // scans input from keyboard
while (sc.hasNext()) {                 // while scanner scans something. (ends with Ctrl + D)
    String stdin = sc.nextLine();       // sets scanned value as string
    System.out.println(stdin);      //prints string

I faced one problem in this section, which was to figure out how to end the program with Ctrl + D. (The original cat command without an argument can be terminated with Ctrl + D)

At first I believed I could use a KeyListener. However, after trying that for two days that didn't turn out very well. 

Then I went to google, did a little search and I finally found out. 

There's something called an End of File (EOF) signal, where it literally tells the computer when it is the end of the program.

When using a Scanner class, by having a while sentence and a hasNext method, you will be able to send an EOF signal by pushing Ctrl + D.

So that was how I was able to replicate the cat command.


In the next article, I will be talking about replicating the ls command. Stay tuned!