I have a particular method where I want to count the number of lines in a text file and then read the file what I am doing is counting the number of lines in the file by iterating over the read.nextLine() and then resetting the buffer and reading from the start of the file again. I am not sure what I am doing wrong
public void ReadFile() throws IOException{
try{
FileReader fr = new FileReader(path);
BufferedReader read = new BufferedReader(fr);
int numberOfLines=0;
while(read.readLine()!= null)
{
numberOfLines++; // Getting the number of lines
}
read.reset();
System.out.println(numberOfLines + ": is the no of lines");
baseString.append(read.readLine());
baseString.append(read.readLine());
baseString.append(read.readLine());
System.out.println(baseString);
}
catch(IOException e){
e.printStackTrace();
}
}
base string is just a Stringbuffer
private StringBuffer baseString = new StringBuffer();
I explicitly need the count to perform some operation