• Post Reply Bookmark Topic Watch Topic
  • New Topic

find multiple occurences of string in file  RSS feed

 
robbie keane
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to find a string in a file in such that if the string is present, I need to print the entire line after that (end of line). How do i find all occurences in a file (indexOf, substring all work for one string).
e.g. if i need to fing string 'test' in a file that has foll content:
****file start****
this is a test for string
here we will check for
a test string in a file
and if we find it
we need to print the
entire test strings.
****file end****

should return me:
test for string
test string in a file
test strings.

Pls suggest if I can find some sample code for this.

Regards
Robbie
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use buffered reader for this.Read a line into a string , then use indexOf function with the key as arguemnt. If its greater than -1 then print the line and proceed with the next line.You can even use some helper functions in apache commons-lang.jar for making a string from the content of a file.
 
robbie keane
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rahul.
How should I PROCEED with the line? can you please send some sample code.

Appreciated.
Robbie
 
robbie keane
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will indexOf work for exact string (case sensitive) , I want the code to look for string in file and the searched string should be case insenstive.

Regards.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use indexof to find all occurrences. Use the signature that includes starting byte. After you find "test" once, look again starting at that location plus the length of "test".

To ignore case, I'd probably make a copy of the input that's all upper case adn do indexOf "TEST".

If you're up to studying regular expressions, there is a CASE_INSENSITIVE option for searches. Google for the Sun Regular Expression tutorial or read the JavaDoc for Pattern.
 
robbie keane
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks but i'm still not able to get that to work. Can you send some sample code for me as a starting point. I need to print the entire line after i've found the text.

Thanks in anticipation.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us your code as a starting point, and we'll help you get it working.
 
robbie keane
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest.
It's working and finding the entire line but now how do i look for each of the directories and sub - directories for the file.
Here's my code:
import java.io.*;

public class Test {

public static void main(String[] args) {
try{
// here specify the directory name
File directory = new File("C:\\Temp\\New Folder");

BufferedWriter out = new BufferedWriter(new FileWriter("outfilename.txt"));;
File[] files = directory.listFiles();

for (int i = 0; i < files.length; i++) {
count(files[i],out);
}
out.close();
}catch (Exception e){
e.printStackTrace();
}
}
private static void count(File fileName, BufferedWriter out) {
BufferedReader in = null;
try {
FileReader fileReader = new FileReader(fileName);
in = new BufferedReader(fileReader);
count(in,out);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}

private static void count(BufferedReader in, BufferedWriter out) throws
IOException {
String line;
try{
do {
line = in.readLine();
if (line != null)
{
ifWordsExists(line,out); // if word exist then write whole line to a new file
}
}
while (line != null);
} catch(Exception e){
e.printStackTrace();
}
}

private static void ifWordsExists(String line,BufferedWriter out) throws IOException{
StringBuffer bf = null;
int index = 0;
while (index < line.length()) {
if(bf == null)
bf = new StringBuffer();
char c = line.charAt(index++);
boolean whitespace = Character.isWhitespace(c);
if (!whitespace) {
bf.append(c);
}else{
if("test".equalsIgnoreCase(bf.toString())){
try {
out.write(line);
out.write("\r\n");
} catch (IOException e) {
e.printStackTrace();
}
bf = null;
break;
}else{
bf = null;
}
}
}
if(bf != null && "test".equalsIgnoreCase(bf.toString())){
out.write(line);
out.write("\r\n");
}
}

}


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