I'm currently learing about the mark() and reset() methods of the BufferedReader and want to do the following:
I parse a CSV, set up like this (spaces as delimiter):
555 value1
555 value2
600 foobar
700 whatever
Each line, one object (adress) is created with the first number as its ID. When the ID is used multiple times, I want to compare the values in the second column and proceed afterwards.
With the data above, my code does compare the two values with ID 555, but fails to create an object with the third line (ID 600).
Mistake found: splitted the first line after parsing the new one. Simple mix up of variables.
My setup is similar to this:
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
br.mark(1000); //save current line
List<String> list = new ArrayList<String>(Arrays.asList(line.split(delimiter)));
Adress a = new Adress(Long.parseLong(list.get(0)); //create object
while (((checkLine = br.readLine()) != null) && exit == false){
List<String> nextLine = new ArrayList<String>(Arrays.asList(line.split(delimiter)));
long nextId = Long.parseLong(nextLine.get(0));
if( nextId == a.getId() ){
System.out.println(nextId + " vs. " + a.getId());
br.mark(1000); //mark the next line if it has the same ID
//[...] do more stuff
} else{
exit = true;
}
}
br.reset(); //reset to the last line with the ID
}
Sysout:
555 vs. 555
555 vs. 555
555 vs. 555