Java program to counting number of lines, words, characters and paragraphs in a text file using Java

Counting the number of characters is important because almost all text boxes that rely on user input have a certain limit on the number of characters that can be inserted. For example, the character limit for Facebook posts is 63,206 characters. However, for tweets on Twitter, the character limit is 140 characters, while the character limit for each post on Snapchat is 80 characters.

Determining character limits become crucial when the tweet and Facebook post updates are being done through api’s.

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.

// Java program to count the
// number of charaters in a file
import java.io.*;

public class Test
{
	public static void main(String[] args) throws IOException
	{
		File file = new File("C:\\Users\\Mayank\\Desktop\\1.txt");
		FileInputStream fileStream = new FileInputStream(file);
		InputStreamReader input = new InputStreamReader(fileStream);
		BufferedReader reader = new BufferedReader(input);
		
		String line;
		
		// Initializing counters
		int countWord = 0;
		int sentenceCount = 0;
		int characterCount = 0;
		int paragraphCount = 1;
		int whitespaceCount = 0;
		
		// Reading line by line from the
		// file until a null is returned
		while((line = reader.readLine()) != null)
		{
			if(line.equals(""))
			{
				paragraphCount++;
			} else {
				characterCount += line.length();
				
				// \\s+ is the space delimiter in java
				String[] wordList = line.split("\\s+");
				
				countWord += wordList.length;
				whitespaceCount += countWord -1;
				
				// [!?.:]+ is the sentence delimiter in java
				String[] sentenceList = line.split("[!?.:]+");
				
				sentenceCount += sentenceList.length;
			}
		}
		
		System.out.println("Total word count = " + countWord);
		System.out.println("Total number of sentences = " + sentenceCount);
		System.out.println("Total number of characters = " + characterCount);
		System.out.println("Number of paragraphs = " + paragraphCount);
		System.out.println("Total number of whitespaces = " + whitespaceCount);
	}
}

Output:

Total word count = 5
Total number of sentences = 3
Total number of characters = 21
Number of paragraphs = 2
Total number of whitespaces = 7

In-built functions used

  1. File(String pathname): java.io.File: Create a new File instance by converting the given pathname string into an abstract pathname.

Syntax:

public File(String pathname)
Parameters:
pathname - A pathname string

2. FileInputStream(File file): java.io.FileInputStream: Create a FileInputStream by opening a connection with the actual file, which is 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.

3. InputStreamReader(InputStream in): java.io.InputStreamReader: Create an InputStreamReader that uses the default character set.

Syntax:

public InputStreamReader(InputStream in)
Parameters:
in - An InputStream

4. BufferedReader(Reader in): java.io.BufferedReader: Creates a character input buffer using the standard input buffer.

Syntax:

public BufferedReader(Reader in)
Parameters:
in - A Reader

 

Submit Your Programming Assignment Details