Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

getting # of lines in file

Posted on 2004-03-28
6
Medium Priority
?
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

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

Author Comment

by:91mustang
ID: 10699258
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 80 total points
ID: 10699282
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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 5

Expert Comment

by:twobitadder
ID: 10699284
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
ID: 10699508
thanks CEHJ
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10699524
8-)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

715 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