为什么这段代码会跳过第一行文件?

时间:2018-01-21 17:08:47

标签: java file

我不止一个地应用了这个代码,这段代码是为了读取一个文件而对于每一行,它应该创建一个新对象并将其添加到att_agreement ArrayList,它对于除第一行之外的每一行都可以正常工作,我不能在输出中找到它的对象。 有什么帮助吗?

public ArrayList<Att_Elements> load_ann(File f) {

    ArrayList<Att_Elements> att_agreement = new ArrayList<Att_Elements>();
    String line="";
    try {
        BufferedReader read = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF8"));

        while((line = read.readLine()) != null) {

            String[] SplitLine = line.split("\\|");


            if (SplitLine[0].equals("Att")) {
                annotation=new Att_Elements();
                annotation.Type = SplitLine[0];
               .
               .
               .
                //...
  att_agreement.add(annotation);
            }
        }

        read.close();
    } catch (IOException e) {
        e.printStackTrace();

    }
    return att_agreement;
}

以下是文件内容示例(3行):         enter image description here

1 个答案:

答案 0 :(得分:2)

您的文件可能具有位于开头的所谓BOM。这是一个字节顺序标记。因此,在BOM不存在的第二行之前,您的条件.equals(&#34; Att&#34;)才会被满足。处理这种情况的单独的if语句应该运行良好。如果您打印每行读取,您应该看到BufferedReader正在读取的第一行。然后可以根据此值定制新的条件语句。

另一种方法是搜索通用物料清单字符串并将其替换为空。