使用BufferedReader跳过每个奇数行?

时间:2012-04-21 15:29:29

标签: java bufferedreader

  

可能重复:
  Reading certain lines only from input?

如果我使用BufferedReader,有没有办法可以跳过文件中的每一个奇数行?

2 个答案:

答案 0 :(得分:1)

只需阅读一行并弃掉它:

BufferedReader bReader = new BufferedReader(new FileReader("someFileName.txt"));
String line = null;

while(true)
{
   //skip the odd line
   bReader.readLine();

   //read an even line
   line = bReader.readLine();

   if(line != null)
      //do stuff with even line
   else
      break; //end of input
}

答案 1 :(得分:0)


BufferedReader br = ...;
String line;
while ((line = br.readLine()) != null) {
    line = br.readLine();
    //do whatever with the data
    if (line == null) break;
}