0

What is the efficient way to read a set of file records from a start line to end line in java. I wrote this but i think it's not elegant:

protected void readPreferenceRecords(int start, int end) throws IOException{
InputStream input; 
int i = 0;
LineNumberReader    reader;
List lines;
String line;
lines = new ArrayList();
input = new FileInputStream(new File(posFile));

reader = new LineNumberReader(new InputStreamReader(input));

while (i < start)  {
  reader.readLine();
  i++;
} if (i == start) {
  while ((line = reader.readLine()) != null && (i <= end)){
lines.add(line);
i++;
  }
}
for (String l : lines) {
  System.out.println(l);
}

}

Amira
  • 3,184
  • 13
  • 60
  • 95

0 Answers0