Solved

getting # of lines in file

Posted on 2004-03-28
6
296 Views
Last Modified: 2010-03-31
hello, how can i get the number of lines in a text file without looping though the lines?
0
Comment
Question by:91mustang
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You can't really. Why would you want to though?
0
 
LVL 4

Author Comment

by:91mustang
Comment Utility
I have an app that reads a file in line parses line, inserts to DB,next line....

I want to use a Jprogress bar and use the number of lines in file as the maximum value for proggress bar
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 20 total points
Comment Utility
You could do something like this:


import java.util.regex.*;
import java.nio.*;
import java.nio.charset.*;
import java.nio.channels.*;
import java.io.*;

/**
 *  Description of the Class
 *
 * @author     CEHJ
 * @created    28 March 2004
 */
public class LineCount2 {

      public static void main(String[] args) throws Exception {
            if (args.length < 1) {
                  System.out.println("Usage: java LineCount2 <text file to count lines of>");
                  System.exit(-1);
            }
            int count = 0;
            FileChannel roChannel = new RandomAccessFile(new File(args[0]), "r").getChannel();
            ByteBuffer bbuf = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) roChannel.size());
            CharBuffer cbuf = Charset.forName(System.getProperty("file.encoding")).newDecoder().decode(bbuf);

            Pattern EOL = Pattern.compile("\\n");
            Matcher matcher = EOL.matcher(cbuf);
            while (matcher.find()) {
                  count++;
            }
            if (cbuf.length() > 0) {
                  ++count;
            }
            System.out.println(count + " lines found");
      }

}
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 5

Expert Comment

by:twobitadder
Comment Utility
You'll need to do a pre-parse/dbInsertion parse to compare every character for '/n' and sum them.
0
 
LVL 4

Author Comment

by:91mustang
Comment Utility
thanks CEHJ
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now