Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

getting the total amount of line in java file

  Asked By: Aysel    Date: Jan 31    Category: Java    Views: 1022
  

how would i go about doing this?
i need to find out the total amount of line in a java file and store
the number somewhere

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Vicki Fields     Answered On: Jan 31

Counting the nos. of lines in a file  is simple, you first create a FileReader
class object on the file that you want to use... then wrap this with
BufferedReader class object... now run a while loop and read the content of the
file line  by line and increment a counter inside the loop. at the end of the
loop the counter will give the number  of lines in the file...

eg

consider your input file is input.txt

write this code in the main method
{
int count = 0;
FileReader fr = new FileReader("input.txt";
BufferedReader br = new BufferedReader(fr);
while(br.readLine() != null)
count++;
System.out.println("The Number of lines in the file is : " + count);
}

 
Didn't find what you were looking for? Find more on getting the total amount of line in java file Or get search suggestion and latest updates.




Tagged: