Reputation: 411
I've a text file with a lot of content in it. I found out ways as to how to search for a word in the text file. But I've an address which has to be searched, and you all know that an address has multiple lines (can be considered as a paragraph) and thats when the problem comes.
How do I give the whole address as input for searching? when I give only one line of the address, i'm able to find the string in the text file since i use nextLine() which reads line by line. Is there a way I can give the whole address as input and search it in the text file.
Upvotes: 0
Views: 575
Reputation: 8101
Yes, you can try what @Oleksi said !. However I would advice to use a StringBuffer
instead of String
.
Something like below should work...
BufferedReader br = new BufferedReader(new FileReader(new File("D:/Shashank/random.txt")));
StringBuffer sb = new StringBuffer("");
String address = "line1\nline2\nline3";
while(br.ready())
{
sb.append(br.readLine());
sb.append("\n");
}
if(sb.indexOf(address)>=0)
{
System.out.println("Address found");
}
Upvotes: 1
Reputation: 13097
You can read the whole file into a string, and then try to find your multi-line address string in that file string. This is efficient since you don't have to perform many file I/O operations, and it will work because String.indexOf(String) to find strings with newline characters in them.
Upvotes: 1