• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read a word/text(not line) from a file?  RSS feed

 
Kew Jie Long
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trying to create a application to search a specific word in the file. In a result, i have to read one word to one word from a file. What should i do? i know that is one method call"readLine" but i don't want read a line but a word. How should i make it happen?


Here is my code:


 
Rodion Gork
Ranch Hand
Posts: 47
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner class is your great friend in this. Use it instead of BufferedReader.

It's method Scanner#next reads characters into string variable up to next delimiter (by default - next space or line feed).
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kew Jie Long wrote:What should i do? i know that is one method call"readLine" but i don't want read a line but a word. How should i make it happen?

You could also split the lines you read in into words. Have a look at the String.split() method.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!