import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReadFile {
public static void main(String[]args) throws IOException{
FileReader in = new FileReader("C:/test.txt");
BufferedReader br = new BufferedReader(in);
String line = "";
String wholeReportText="";
while ((line = br.readLine()) != null) {
wholeReportText += line;
}
in.close();
Pattern pattern = Pattern.compile("Movies(.*?)");
Matcher matcher = pattern.matcher(wholeReportText);
// check all occurance
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Pattern daypat = Pattern.compile("Tuesday (.*?)");
Matcher daymat = daypat.matcher(wholeReportText);
// check all occurance
while (daymat.find()) {
System.out.println(matcher.group(1));
}
}
}
您的问题需要分解为子问题,例如算法
Step 1) To list all the files which you need to parse (File API in
java FileFilter in java NIO and Paths API in java7)
Step 2) Read and extract data from file one by one (sample code
above)
Step 3) Store the extracted data in some HashMap or ArrayList
Step 4) Use collections as per your requirement