0

I have a question about Java:

I am trying to make a program that goes through files and checks something, but for that I need something to count the lines, does anyone knows a good method to do this?

I have never worked with files before so I dont really know something. Also I don't have code to show, because I don't know how to do it.

Dogyman
  • 57
  • 8

5 Answers5

3

By searching on the Internet you could have found this by yourself. But nevertheless, here is a code that I use:

    public static int countLines(String filename) throws IOException {
        InputStream is = new BufferedInputStream(new FileInputStream(filename));
        try {
            byte[] c = new byte[1024];
            int count = 0;
            int readChars = 0;
            boolean empty = true;
            while ((readChars = is.read(c)) != -1) {
                empty = false;
                for (int i = 0; i < readChars; ++i) {
                    if (c[i] == '\n') {
                        ++count;
                    }
                }
            }
            return (count == 0 && !empty) ? 1 : count;
        } 
        finally {
            is.close();
        }
    }

I hope it works for you.

Update:

This should be easier for you.

BufferedReader reader = new BufferedReader(new FileReader(file));
int lines = 0;
while (reader.readLine() != null) lines++;
reader.close();
System.out.println(lines);
JetStream
  • 809
  • 1
  • 8
  • 28
  • Well, that was really fast, thank you! It seems to work (testet it shortly in the main method). – Dogyman Jul 16 '15 at 06:54
  • This is a really poor and convoluted way to count lines. I'd imagine doing something like this back when I was writing Java 1.2, but even then I'd use a `BufferedReader`. The only advantage here is that you can't break it with long lines (I'm talking 2GB or so), but that's rarely an issue. – Kayaman Jul 16 '15 at 07:02
  • @Kayaman Still it works so I'm going to use it, I will test both methods but both seem to be right :D – Dogyman Jul 16 '15 at 07:05
  • @Kayaman Yeah I know that this method is old but I still use it, don't ask me why its just preferred by me – JetStream Jul 16 '15 at 07:08
1

In Java 8:

long lineCount = 0;

try (Stream<String> lines = Files.lines(Paths.get(filename))){
    lineCount = lines.count();
} catch (final IOException i) {
    // Handle exception.
}
marstran
  • 26,413
  • 5
  • 61
  • 67
0

You can try this, in lines variable you will get number of lines.

 public String getFileStream(final String inputFile) {
            int lines = 0;
            Scanner s = null;

            try {
                s = new Scanner(new BufferedReader(new FileReader(inputFile)));
                while (s.hasNext()) {
                   lines++;
                }
            } catch (final IOException ex) {
                ex.printStackTrace();
            } finally {
                if (s != null) {
                    s.close();
                }
            }
            return result;
    }
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
0

Simply you can do this :-

        BufferedReader br = new BufferedReader(new FileReader(FILEPATH));
        int lineCount = 0;
        while(br.readLine() != null)
            lineCount++;
        System.out.println(lineCount);

BufferedReader class provides readLine() which reads the text line-by-line so this can be used to get the lines count.

AnkeyNigam
  • 2,810
  • 4
  • 15
  • 23
0

you can do simply :

public static void main(String[] args) {

    int linecount = 0;

    try {
        // Open the file
        FileInputStream fstream = new FileInputStream("d:\\data.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(
                fstream));
        String strLine;
        // Read File Line By Line
        while ((strLine = br.readLine()) != null) {
            if (strLine.trim().length() > 0) { // check for blank line
                linecount++;
            } else {
                continue;
            }
        }

        System.out.println("Total no. of lines = " + linecount);
        // Close the input stream
        br.close();
    } catch (Exception e) {
        // TODO: handle exception
    }

}
Rustam
  • 6,485
  • 1
  • 25
  • 25