缓冲读取器代码读取输入文件

时间:2015-08-31 17:01:42

标签: java if-statement bufferedreader tokenize

我有一个名为“message.txt”的文本文件,使用Buffer Reader读取。文本文件的每一行都包含“word”和“含义”,如本例所示:

“PS:小学”

其中PS - 小学,小学 - 意思

当正在读取文件时,每一行都被标记为“word”,并且“from”来自“:”。 如果“含义”等于名为“f_msg3”的给定输入字符串,则在名为“txtView”的文本视图上显示“f_msg3”。否则,它在文本视图上显示“f_msg”。

但是“if condition”在此代码中无法正常工作。例如,如果“f_msg3”等于“小学”,则文本视图上的输出必须为“小学”。但它输出为“f_msg”而不是“f_msg3”。 (“f_msg3”不包含任何不必要的字符串。)

有人可以解释我哪里出错吗?

try {
    BufferedReader file = new BufferedReader(new InputStreamReader(getAssets().open("message.txt")));
    String line = "";

    while ((line = file.readLine()) != null) {
    try {
    /*separate the line into two strings at the ":" */
    StringTokenizer tokens = new StringTokenizer(line, ":"); 
    String word = tokens.nextToken();
    String meaning = tokens.nextToken();

    /*compare the given input with the meaning of the read line */
    if(meaning.equalsIgnoreCase(f_msg3)) {
    txtView.setText(f_msg3);
    } else {
    txtView.setText(f_msg);
    }
    } catch (Exception e)  {
    txtView.setText("Cannot break");
    }
    }

}   catch (IOException e) {
    txtView.setText("File not found");
}

4 个答案:

答案 0 :(得分:1)

我的代码中没有发现任何明显错误,也许这只是一个问题 在比较之前清理字符串(即删除标题和尾随空格,换行符等)。 尝试修剪meaning,例如像这样:

...
String meaning = tokens.nextToken();

if(meaning != null) {
    meaning = meaning.trim();
}

if(f_msg3.equalsIgnoreCase(meaning)) {
    txtView.setText(f_msg3);
} else {
    txtView.setText(f_msg);
}
...

答案 1 :(得分:1)

StringTokenizer负责处理数字(导致错误的原因)和其他“令牌” - 因此可能会被视为调用过多的复杂性。

String[] pair = line.split("\\s*\\:\\s*", 2);
if (pair.length == 2) {
    String word = pair[0];
    String meaning = pair[1];
    ...
}

这会使用正则表达式将行拆分为最多2个部分(第二个可选参数)。 \s*代表任何空格:制表符和空格。

您也可以在属性中加载所有内容。在属性文件中,格式key=value是惯例,但也允许key:value。然而,可能需要一些逃避。

答案 2 :(得分:1)

试试这个

............
meaning = meaning.replaceAll("\\s+", " ");

/*compare the given input with the meaning of the read line */
if(meaning.equalsIgnoreCase(f_msg3)) {
     txtView.setText(f_msg3);
} else {
     txtView.setText(f_msg);
}
............

否则评论其他部分,然后就可以了。

答案 3 :(得分:0)

ArrayList vals = new ArrayList();

    String jmeno = "Adam";

    vals.add("Honza");
    vals.add("Petr");
    vals.add("Jan");


        if(!(vals.contains(jmeno))){
            vals.add(jmeno);
        }else{
            System.out.println("Adam je už v seznamu");
        }


        for (String jmena : vals){
            System.out.println(jmena);
        }


        try (BufferedReader br = new BufferedReader(new FileReader("dokument.txt"))) 
    {

      String aktualni = br.readLine();
      int pocetPruchodu = 0;
        while (aktualni != null) 
        {
            String[] znak = aktualni.split(";");
            System.out.println(znak[pocetPruchodu] + " " +znak[pocetPruchodu + 1]);
            aktualni = br.readLine();
        }
        br.close();
    }
    catch (IOException e) 
    {
            System.out.println("Nezdařilo se");
    }

        try (BufferedWriter bw = new BufferedWriter(new FileWriter("dokument2.txt"))) 
    {


      int pocetpr = 0;
        while (pocetpr < vals.size()) 
        {

                bw.write(vals.get(pocetpr));
                bw.append(" ");

            pocetpr++;
        }
        bw.close();
    }
    catch (IOException e) 
    {
            System.out.println("Nezdařilo se");
    }
相关问题