DEV Community

Anand C
Anand C

Posted on

๐Ÿงฎ Build Your Own `wc` Tool in Java โ€” A Minimal Clone of the Unix Word Count Utility

If you've ever used a Linux terminal, you're probably familiar with the wc command โ€” a simple yet powerful utility that prints the number of lines, words, and bytes (or characters) in a file or input stream. As part of my learning journey in Java and systems programming, I decided to implement a basic clone of the wc tool in Java.

In this blog post, I'll walk you through what I've built, what works, what doesn't (yet), and share the code for anyone looking to experiment or contribute.


๐Ÿš€ Project Overview

๐Ÿ”— GitHub Repository: github.com/anandshaji679322/WC-Tool

โœ… Features Implemented

This tool supports the following options, just like the original wc:

  • -c: Count bytes
  • -m: Count characters
  • -w: Count words
  • -l: Count lines

It works both with file input and standard input (stdin) for most flags.

๐Ÿ› ๏ธ Technologies Used

  • Java
  • Basic I/O: FileInputStream, FileReader, Scanner
  • Stream handling with System.in and custom logic

๐Ÿ“ฆ How to Use

1. Count bytes in a file:

java ccwc -c test.txt

Enter fullscreen mode Exit fullscreen mode

2. Count words from stdin:

cat test.txt | java ccwc -w

Enter fullscreen mode Exit fullscreen mode

3. Count lines from stdin:

cat test.txt | java ccwc -l
Enter fullscreen mode Exit fullscreen mode

4. Count characters from stdin:

cat test.txt | java ccwc -m  # โŒ Not yet fully working
Enter fullscreen mode Exit fullscreen mode

โš ๏ธ Known Issues

๐Ÿงช Currently, the -c (byte count) and -m (character count) flags do not work correctly when used with stdin (cat file | java ccwc -c).

I attempted different approaches to fix this issue, including using BufferedOutputStream and InputStreamReader to handle the input stream, but both methods did not resolve the problem.


๐Ÿ” Code Highlights

Hereโ€™s an example of how I count words from a file:

static int wordCountFile(String fileName) throws FileNotFoundException {
    int count = 0;
    try (Scanner sc = new Scanner(new File(fileName))) {
        while (sc.hasNext()) {
            count++;
            sc.next();
        }
    }
    return count;
}

Enter fullscreen mode Exit fullscreen mode

๐Ÿ“š What I Learned

  • Handling standard input (System.in) in Java requires careful stream management.
  • When designing CLI tools, itโ€™s important to handle edge cases like invalid flags and argument lengths.
  • Java's Scanner is a simple but effective class for parsing input.
  • Working with byte vs character streams teaches you a lot about encoding.

๐Ÿ“ˆ Whatโ€™s Next

If I revisit this project, I want to:

  • โœ… Fix the stdin issue for -c and -m
  • โž• Add support for multiple flags at once (like wc -cwm file.txt)
  • ๐Ÿ“ฆ Package it as a standalone .jar with command-line installation instructions

๐Ÿ‘จโ€๐Ÿ’ป Conclusion

This was a great mini-project that helped me get hands-on with Javaโ€™s I/O system while building a tool similar to a real-world Unix utility.

If you're learning Java or preparing for systems programming interviews, I highly recommend trying something similar!

Feel free to fork the project or contribute on GitHub:

๐Ÿ”— github.com/anandshaji679322/WC-Tool

Top comments (1)

Collapse
 
xzel profile image
Axel Howind

Nice way to practice your skills. What you should change right away:

  • always use uppercase class names
  • If I have seen correctly, main() cannot throw IOException, remove the throws clause

And if you want to improve further, you can easily cut the size in half by using the same counting methods for stdin and file arguments. Maybe itโ€™s not too obvious how to do it, but just give it a try.