java bufferreader读取元素等于另一个数组元素

时间:2014-04-07 20:46:03

标签: java arrays compare filestream readline

我使用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
}

2 个答案:

答案 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;
        }
    }
}