parsing - Finding String in a list of text files in a directory Java ..want to locate line number. -


hi trying add functionality can keep track of line number of string found. not sure , how implement in following code. keep in mind files looking 50mb in size.

import java.io.bufferedreader; import java.io.file; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception;   public class scanfiles {     private static void scanfiles(string folderpath, string searchstring) throws filenotfoundexception, ioexception {         file folder = new file(folderpath);          if (folder.isdirectory()) {             (file file : folder.listfiles()) {                 if (!file.isdirectory()) {                     bufferedreader br = new bufferedreader(new filereader(file));                     string content = "";                     try {                         stringbuilder sb = new stringbuilder();                         string line = br.readline();                          while (line != null) {                             sb.append(line);                             sb.append(system.lineseparator());                             line = br.readline();                         }                         content = sb.tostring();                       } {                         br.close();                     }                     if (content.contains(searchstring)) {                         system.out.println("file " + file.getname() + " contains searchstring " + searchstring + "!");                     }                  }             }         } else {             system.out.println("not directory!");         }     }     public static void main(string args[]) throws filenotfoundexception, ioexception{         scanfiles(new file("c://users//farmania//documents//cob nak messages").getabsolutepath(),"15:09:07,803");  } } 

search text loop on each line of file while reading. keep record of line numbers string found. example:

list<integer> linenumbers = new arraylist<integer>(); int linenumber = 0; while ( line != null ){     linenumber++;     if ( line.indexof(searchstring) != -1 ){         linenumbers.add(linenumber);     }     line = br.readline(); } if ( linenumbers.size() > 0 ){     //do them } 

depending upon context, may wish case insensitive search changing search string , each line particular case (uppercase/lowercase).


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -