• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to check if a string exists in a file  RSS feed

 
AJ Smith
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for eg. to search the string "abcde" in "text.txt"
if found, println("found .")
else , println("not found .")
thanx !
[ August 15, 2003: Message edited by: AJ Smith ]
 
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
Hi AJ,
Well, there's more than one way you could do this, but general, you need to:
1) Read in the file, line by line
2) Check each line for the pattern
3) If you find the pattern, print the "found" message
4) At the end, if you never found it, print the "not found" message.
You can do 1) using a FileReader and a BufferedReader working together:


To check a string for the pattern, you can use the String.indexOf() method.
 
AJ Smith
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got it.
thx !
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!