Java Program to Count the Number of Lines, Words, Characters, and Paragraphs in a Text File - GeeksforGeeks

05 Oct, 2021
Counting the number of characters is essential because almost all the text boxes that rely on user input have certain limitations on the number of characters inserted. For example, the character limit on a Facebook post is 63206 characters. Whereas for a tweet on Twitter, the character limit is 140 characters, and the character limit is 80 per post for Snapchat.
Determining character limits become crucial when the tweet and Facebook post updates are being done through APIs. 
This function is present under the java.io.File package. It creates a new File instance by converting the given pathname string into an abstract pathname. 
Syntax: 
public File(String pathname)
Parameters:
pathname - A pathname string
This function is present under the java.io.FileInputStream package. It creates a FileInputStream by opening a connection to an actual file named by the File object file in the file system. 
Syntax: 
public FileInputStream(File file) throws FileNotFoundException
Parameters:
file - the file to be opened for reading.
Throws:
FileNotFoundException – if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
SecurityException – if a security manager exists and its checkRead method denies read access to the file.
This function is present under the java.io.InputStreamReader package. It creates an InputStreamReader that uses the default charset. 
Syntax: 
public InputStreamReader(InputStream in)
Parameters:
in - An InputStream
This function is present under the java.io.BufferedReader package. It creates a buffering character-input stream that uses a default-sized input buffer. 
Syntax: 
public BufferedReader(Reader in)
Parameters:
in - A Reader
Java
// Java program to count the// number of lines, words, sentences,  // characters, and whitespaces in a fileimport java.io.*;  public class Test {    public static void main(String[] args)        throws IOException    {        File file = new File("C:\\Users\\hp\\Desktop\\TextReader.txt");        FileInputStream fileInputStream = new FileInputStream(file);        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);          String line;        int wordCount = 0;        int characterCount = 0;        int paraCount = 0;        int whiteSpaceCount = 0;        int sentenceCount = 0;          while ((line = bufferedReader.readLine()) != null) {            if (line.equals("")) {                paraCount += 1;            }            else {                characterCount += line.length();                String words[] = line.split("\\s+");                wordCount += words.length;                whiteSpaceCount += wordCount - 1;                String sentence[] = line.split("[!?.:]+");                sentenceCount += sentence.length;            }        }        if (sentenceCount >= 1) {            paraCount++;        }        System.out.println("Total word count = "+ wordCount);        System.out.println("Total number of sentences = "+ sentenceCount);        System.out.println("Total number of characters = "+ characterCount);        System.out.println("Number of paragraphs = "+ paraCount);        System.out.println("Total number of whitespaces = "+ whiteSpaceCount);    }}
The TextReader.txt file contains the following data – 
Hello Geeks. My name is Nishkarsh Gandhi.
GeeksforGeeks is a Computer Science portal for geeks.
Output:
Note: This program would not run on online compilers. Please make a txt file on your system and give its path to run this program on your system. 
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect or you want to share more information about the topic discussed above.
UmeshAgrawal
sagar0719kumar
sahilsehgals4392
nishkarshgandhi
java-file-handling
Java-I/O
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Interfaces in Java
Singleton Class in Java
Multithreading in Java
Set in Java
Collections in Java
Queue Interface In Java
Initializing a List in Java
Different ways of Reading a text file in Java
Constructors in Java
Inheritance in Java