Today we will learn how to get number of rows in file using java

Lets create file with 11 rows on disk C

And run this code:


try{

File myFile =new File("c:\\fileWith11Line.txt");
FileReader fileReader = new FileReader(myFile);
LineNumberReader lineNumberReader = new LineNumberReader(fileReader);

int lineNumber = 0;

while (lineNumberReader.readLine() != null){
lineNumber++;
}

System.out.println(lineNumber);

lineNumberReader.close();

}catch(IOException e){
e.printStackTrace();
}

Result : 11