我使用bufferreader读取文件,还有另一个数组(array_to_compare_with)。所以,我想比较这两个数据 我怎么能克服这个?有人有线索吗?
String[] array_to_compare_with = { "str1","str2","str3","str4" }
FileInputStream fstream = new FileInputStream("filename");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
// strLine and array_to_compare_with comparison needed to be done
}
答案 0 :(得分:0)
也许你想要这样的东西:
String[] array_to_compare_with = { "str1","str2","str3","str4" };
FileInputStream fstream = new FileInputStream("filename");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
boolean found = false;
for(String str : array_to_compare_with){
if(str.equalsIgnoreCase(strLine.trim())){
found = true;
break;
}
}
if(found){
//DO WHAT YOU WHANT
}
}
答案 1 :(得分:0)
String[] array_to_compare_with = { "str1","str2","str3","str4" }
FileInputStream fstream = new FileInputStream("filename");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
for(String s: array_to_compare_with){
if(strLine.contains(s)){
System.out.println(strLine);
break;
}
}
}