0

I applied this code more than one, this code is to read a file and for each line, it should create a new object and add it to att_agreement ArrayList, it works fine for each line except the first line, I can not find its object in the output. Any help, please?

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

Here is a sample of file content (3 lines): enter image description here

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
Abeer A
  • 45
  • 5

1 Answers1

2

Your file likely has what is called a BOM located at the beginning. This is a byte order mark. Thus, your conditional .equals("Att") is not being met until the second line where the BOM is not present. A separate if statement to handle this case should work well. If you print each line read, you should see what the BufferedReader is reading as the first line. The new conditional statement can then be tailored to this value.

Another approach is to search for a generic BOM string and replace it with nothing.

sellc
  • 380
  • 1
  • 5
  • 19