日期:2014-05-20  浏览次数:20776 次

java判断匹配的问题

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public class PiPei {
public static void ReadData() {
try {

FileReader read1 = new FileReader("d:/RunLog2011-04-02.txt");
BufferedReader br1 = new BufferedReader(read1);
FileReader read2 = new FileReader("d:/date1.txt");
BufferedReader br2 = new BufferedReader(read2);
PrintWriter write=new PrintWriter("d:/date2.txt");
String s1 = null;
String s2 = null;

while ((s2 = br2.readLine()) != null) {
// System.out.println(s2);
while ((s1 = br1.readLine()) != null) {
// System.out.println(s1);
if(s1.indexOf(s2)>=0){

System.out.println("---"+ s1);
write.println(s1);

}

write.flush();
}
// U9811040215413780156
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args) {
ReadData();
}
}

请问为什么只会拿第一个取出来的数据去匹配呢?第二个如果相同也不会显示,请帮忙看看!谢谢


------解决方案--------------------
Java code




import java.io.*;
import java.util.*;

public class Test {
public static void ReadData() {
try {

FileReader read1 = new FileReader("d:/input.txt");
BufferedReader br1 = new BufferedReader(read1);
FileReader read2 = new FileReader("d:/condition.txt");
BufferedReader br2 = new BufferedReader(read2);
PrintWriter write=new PrintWriter("d:/output.txt");
String s1 = null;
String s2 = null;

while ((s2 = br2.readLine()) != null) {
System.out.println(s2);


br1.mark(100);//注意这里



while ((s1 = br1.readLine()) != null) {
System.out.println(s1);
if(s1.indexOf(s2)>=0){

System.out.println("---"+ s1);
write.println(s1);

}


write.flush();
}


br1.reset();//注意这里


//     U9811040215413780156
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args) {
ReadData();
}
}