Recently I got an email asking for “Can I have a tutorial on counting total number of words
, lines
and characters
from file?”
Some time back I’ve written an article on How to Read a File Line by Line in Reverse Order which doesn’t print above requested metrics. In this tutorial we will go over steps on how to print total number of Characters, Words and Lines for a given file.
Let’s get started:
- We will read file
index.php
first located at/Users/<username>/Downloads
. I’m running program on my Macbook Pro so replace username with your actual name - We will read each line and will remove multiple whitespace using regex
\\s
- Print file and results on console
If you need more information on REGEX pattern in java then follow below tutorials:
- Matcher (java.util.regex.Matcher) Tutorial
- What is RegEx Pattern (Regular Expression)? How to use it in Java?
File we are using:
<?php // Silence is golden. This is Crunchify's tutorial which counts total number of char, words and lines... ?>
Java code:
package com.crunchify.tutorials; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; /** * @author Crunchify.com * */ public class CrunchifyCountWordsLineCharacters { public static void readFileAndPrintCounts(String crunchifyFile) throws FileNotFoundException { BufferedReader crunchifyBuffer = null; int crunchifyTotalWords = 0; int crunchifyTotalLines = 0; int crunchifyTotalCharacters = 0; String crunchifyLine; // Read file contents crunchifyBuffer = new BufferedReader(new FileReader(crunchifyFile)); try { crunchifyLog("========== File Content =========="); // read each line one by one while ((crunchifyLine = crunchifyBuffer.readLine()) != null) { crunchifyLog(crunchifyLine); crunchifyTotalLines++; // ignore multiple white spaces String[] myWords = crunchifyLine.replaceAll("\\s+", " ").split(" "); for (String s : myWords) { crunchifyTotalCharacters += s.length(); } crunchifyTotalWords += myWords.length; } } catch (IOException e) { e.printStackTrace(); } crunchifyLog("\n========== Result =========="); crunchifyLog("* Total Characters: " + crunchifyTotalCharacters); crunchifyLog("* Total Words: " + crunchifyTotalWords); crunchifyLog("* Total Lines: " + crunchifyTotalLines); } private static void crunchifyLog(String string) { System.out.println(string); } public static void main(String[] args) { try { String crunchifyFile = "/Users/<username>/Downloads/index.php"; readFileAndPrintCounts(crunchifyFile); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
Console Output:
========== File Content ========== <?php // Silence is golden. This is Crunchify's tutorial which counts total number of char, words and lines... ?> ========== Result ========== * Total Characters: 95 * Total Words: 21 * Total Lines: 6
Have anything to add to this article? Please chime in and join the conversion.
The post How to read File in Java and Count total number of Characters, Words and Lines appeared first on Crunchify.
Author: App Shah