• Post Reply Bookmark Topic Watch Topic
  • New Topic

File Reading  RSS feed

 
Anudeep Duvvuri
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I want to know how to display a particular line which matches a specified word in the line
Example This is my file
1 Sachin 100
2 Sehwag 22
3 Dravid 55
4 Dhoni 66

I want to print the line 3 i.e 3 Dravid 55

Please reply me
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried yourself so far?

You can easily find example code for reading a text file line by line in a Java program. From there, it shouldn't be too hard to change the program to do what you want it to do exactly. Give it a try and let us know if it works.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer to see the reader closed in a finally block. Look at this thread. Note there is another idiom you would never guess, about while ((line = br.nextLine()) != null)...

Of course, if you use Java7, you can simplify things no end with “try-with-resources”. You can use a similar idiom for closing readers, writers, socket connections, database connections, etc. As long as you don’t close a reader pointing to System.in or similar.
 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a while Loop to read the Entire file using a buffered Reader and then Select the Line you want.

Pseudocode:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!