Handle Blank Spaces Reading in Text Files Java

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 mail service for Snapchat.

Determining character limits become crucial when the tweet and Facebook mail updates are beingness washed through APIs.

In-congenital Functions Used

1. File(String pathname):

This office is nowadays under the java.io.File package. It creates a new File case past converting the given pathname string into an abstract pathname.

Syntax:

public File(String pathname)

Parameters:

pathname - A pathname string

2. FileInputStream(File file) :

This role is present nether the java.io.FileInputStream package. It creates a FileInputStream by opening a connexion to an bodily file named by the File object file in the file system.

Syntax:

public FileInputStream(File file) throws FileNotFoundException

Parameters:

file - the file to exist opened for reading.

Throws:

  • FileNotFoundException – if the file does non exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
  • SecurityException – if a security managing director exists and its checkRead method denies read access to the file.

three. InputStreamReader(InputStream in):

This role is present under the coffee.io.InputStreamReader package. It creates an InputStreamReader that uses the default charset.

Syntax:

public InputStreamReader(InputStream in)

Parameters:

in - An InputStream

iv. BufferedReader(Reader in):

This function is present under the java.io.BufferedReader package. Information technology creates a buffering character-input stream that uses a default-sized input buffer.

Syntax:

public BufferedReader(Reader in)

Parameters:

in - A Reader

Example:

Java

import coffee.io.*;

public class Examination {

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.divide( "\\s+" );

wordCount += words.length;

whiteSpaceCount += wordCount - i ;

Cord sentence[] = line.dissever( "[!?.:]+" );

sentenceCount += sentence.length;

}

}

if (sentenceCount >= 1 ) {

paraCount++;

}

System.out.println( "Full 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 –

Hi Geeks. My name is Nishkarsh Gandhi. GeeksforGeeks is a Calculator Scientific discipline portal for geeks.

Output:

Notation: This program would not run on online compilers. Delight make a txt file on your arrangement and give its path to run this program on your system.

This article is contributed by Mayank Kumar. If you lot similar GeeksforGeeks and would like to contribute, you can write an article using write.geeksforgeeks.org or postal service your article to review-team@geeksforgeeks.org. Run into your article actualization on the GeeksforGeeks primary page and assist other Geeks. Please write comments if y'all find anything incorrect or you want to share more information virtually the topic discussed above.


fockenanch1969.blogspot.com

Source: https://www.geeksforgeeks.org/java-program-to-count-the-number-of-lines-words-characters-and-paragraphs-in-a-text-file/

0 Response to "Handle Blank Spaces Reading in Text Files Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel