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
2. Count words from stdin:
cat test.txt | java ccwc -w
3. Count lines from stdin:
cat test.txt | java ccwc -l
4. Count characters from stdin:
cat test.txt | java ccwc -m # โ Not yet fully working
โ ๏ธ 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;
}
๐ 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)
Nice way to practice your skills. What you should change right away:
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.