Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Pls HELP! How to count the number of row in file?  RSS feed

 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to limit the number of row to read from a log file like max is 1000.
How do I do this?
This class is read the whole log file, Many thanks to your help or hints.
Regards.


 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't seem like a four-heads-beating-on-the-wall problem to me, but in any case...

You have a single "while" loop that reads lines. There's a "null" check to check for the null that you'll get on the end of file. What you need to do is make the loop also contingent on having read less than 1000 lines, something like

while (++lineCount < 1000 && ((line = input.readLine()) != null) { ...

You'd have to declare "lineCount", of course.
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi :Ernest Friedman-Hill
Thanks for a quick response. Let me try it out.
Also, another question: since I am reading a log file, it is have the following pattern:
[date][INFO][some detail description] or
[date][DEBUG][some debug description]
[date][ERROR][some error description] (see sample)


I want to retrieve only for example ERROR and WARNING lines with limit to 1000 rows (as you already provided the solution), How do I extract the lines with only those words and display back to user.? Many thanks as always.!!!

 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



In the above code, line variable is nothing but string, so use indexOf method like (line.indexOf("ERROR") > 0 || line.indexOf("DEBUG") > 0) then write to your destination otherwise go for next iteration.

hth,
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Prabhu Venkatachalam
Regards
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi : I have posted my code previously, the way the code is reading file top to bottom, how do I read the file backward? Because the log is capture the most current information at the bottom of the file.

Again, as always, I really am appreciated for your kindness HELP!!
Regards
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!