Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7631995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:30:12+00:00 2026-05-31T06:30:12+00:00

If I read a file the have empty spaces between paragraphs, how do I

  • 0

If I read a file the have empty spaces between paragraphs, how do I count the empty spaces and subtract them from the total line count?

public int countLinesInFile(File f){
    int lines = 0;
    try {
        BufferedReader reader = new BufferedReader(new FileReader(f));

        while (reader.readLine() != null){
            lines++;    
        }
        reader.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return lines;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:30:14+00:00Added an answer on May 31, 2026 at 6:30 am

    To see if a line is blank :

     int blankLines = 0;
     String lineRead = "";
     while ( lineRead != null ){
           lineRead = reader.readLine();
           //on newer JDK, use lineRead.isEmpty() instead of equals( "" )
           if( lineRead != null && lineRead.trim().equals( "" ) ) {
            blankLines ++;
           }//if 
           lines++;    
     }//while
    
     int totalLineCount = lines - blankLines;
    

    And to respect the excellent advices of @JB Nizet :

    /**
     * Count lines in a text files. Blank lines will not be counted.
     * @param f the file to count lines in.
     * @return the number of lines of the text file, minus the blank lines.
     * @throws FileNotFoundException if f can't be found.
     * @throws IOException if something bad happens while reading the file or closing it.
     */
    public int countLinesInFile(File f) throws FileNotFoundException, IOException {
        BufferedReader reader = null;
        int lines = 0;
        int blankLines = 0;
        try {
                reader = new BufferedReader(new FileReader(f));
    
                String lineRead = "";
                while ( lineRead != null ){
                   lineRead = reader.readLine();
                   //on newer JDK, use lineRead.isEmpty() instead of equals( "" )
                   if( lineRead != null && lineRead.trim().equals( "" ) ) {
                      blankLines ++;
                   }//if 
                   lines++;    
                }//while
        } finally {
           if( reader != null ) {
              reader.close();
           }//if
        }//fin
        return lines - blankLines;
    }//met
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a while loop which reads lines from a file using read line.
I'm trying to read a file from a local filesystem. I do not have
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
I have read a file into a String. The file contains various names, one
I have to read a file in the BUILD method and I want to
I have semicolon-separated .dat file and I want to read that file and store
I have a text file. I want read that file. But In that if
My current situation is: I have to read a file and put the contents
I have to read a binary file in a legacy format with Java. In
I have to read a txt file with lines formated like this: 1: (G,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.