I am reading large log files using BufferedReader in java.I have to filter the contents of the file and store the data in database. example.
BufferedReader br=new BufferedReader(new FileReader("test.log"));
String line;
while((line=br.readLine())!=null){
if(line.contains("filter1") || line.contains("filter2") ||
line.contains("filter3") || line.contains("filter4")...){
//creating object and storing using hibernate
}
}
I have more than 50 such filters and the problem occurs in reading files over 100 MB. A lot of time is wasted in matching these filter strings.
I cannot use Collection.contains(line) as the filters in if conditions are substrings of the line read. The time taken is not due to IO but the filtering of contents and creating objects for storing.
Edit 1 :- filter1, filter2 are just for simplicity only. In actual cases, the filter would be like - "new file", "report","removed from folder","schema","move","copy","added to queue","unique id" etc. These are the specific keyword that I check to see if the line contains relevant data for storing.
Please suggest a better way for achieving the same.
In Java 8, you can use Files.lines to read file as Stream.
This example shows you how to use Stream to filter content, convert the entire content to upper case and return it as a List.