It maybe a better option here to use the LineNumberReader class for counting and reading the lines of text. Although this isn't the most efficient way for counting lines in a file (according to this question) it should suffice for most applications.
From LineNumberReader for readLine method:
Read a line of text. Whenever a line terminator is read the current line number is incremented. (A line terminator is usually a newline character '\n' or a carriage return '\r').
This means that when you call the getLineNumber method of the LineNumberReader class, it will return the current line number that has been incremented by the readLine method.
I have included comments in the code below explaining it.
System.out.println ("Counting ...");
InputStream stream = ParseTextFile.class.getResourceAsStream("/test.txt");
InputStreamReader r = new InputStreamReader(stream);
/*
* using a LineNumberReader allows you to get the current
* line number once the end of the file has been reached,
* without having to increment your original 'count' variable.
*/
LineNumberReader br = new LineNumberReader(r);
String line = br.readLine();
// use a long in case you use a large text file
long wordCount = 0;
while (line != null) {
String[] parts = line.split(" ");
wordCount+= parts.length;
line = br.readLine();
}
/* get the current line number; will be the last line
* due to the above loop going to the end of the file.
*/
int lineCount = br.getLineNumber();
System.out.println("words: " + wordCount + " lines: " + lineCount);