WordCount is a Java application that mimics the functionality of the Unix wc
command. It counts lines, words, bytes, and characters in files or input streams.
To build the software, follow these steps:
- Ensure you have Java Development Kit (JDK) 11 or later installed on your system.
- Ensure you have Apache Maven installed on your system.
- Clone this repository or download the source code.
- Navigate to the project root directory in your terminal.
- Build the project using Maven:
mvn clean package
- The compiled JAR file will be in the
target
directory.
After building, you can run the WordCount application using the following syntax:
java -jar target/wordcount-1.0-SNAPSHOT.jar [options] [file]
-c
: Count bytes.-l
: Count lines.-w
: Count words.-m
: Count characters.
If a file is specified as the last argument, the application will count the specified type of tokens in that file.
If no file is specified, the application will read from standard input.
-
Count lines in a file:
java -jar target/wordcount-1.0-SNAPSHOT.jar -l input.txt
-
Count words in a file:
java -jar target/wordcount-1.0-SNAPSHOT.jar -w input.txt
-
Count bytes in a file:
java -jar target/wordcount-1.0-SNAPSHOT.jar -c input.txt
-
Count characters in a file:
java -jar target/wordcount-1.0-SNAPSHOT.jar -m input.txt
-
Count lines in a string:
echo "Hello, World!" | java -jar target/wordcount-1.0-SNAPSHOT.jar -l
-
Count words in a string:
echo "Hello, World!" | java -jar target/wordcount-1.0-SNAPSHOT.jar -w
-
Count bytes in a string:
echo "Hello, World!" | java -jar target/wordcount-1.0-SNAPSHOT.jar -c
-
Count characters in a string:
echo "Hello, World!" | java -jar target/wordcount-1.0-SNAPSHOT.jar -m
-
Count lines, words, bytes, and characters in a file:
java -jar target/wordcount-1.0-SNAPSHOT.jar input.txt
-
Count lines, words, bytes, and characters in a string:
echo "Hello, World!" | java -jar target/wordcount-1.0-SNAPSHOT.jar