Solved

getting # of lines in file

Posted on 2004-03-28
6
308 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
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 20 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse IDE - Cannot copy/paste from console output 8 124
Java array passed to SQL where clause 23 61
Java Jpanels and Jframe 8 28
split string containing \r\n in Java 46 38
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

932 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

13 Experts available now in Live!

Get 1:1 Help Now