使FileReader使用循环读取每四行

时间:2012-03-06 18:28:06

标签: java loops while-loop bufferedreader

我的问题我必须安排,在搜索客户时,安排while循环只检查每四行阅读。

这是我已经解决过这个问题的代码:

BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;

while ((line = br.readLine()) != null)
{
    ...
}

br.close();

有人知道“......”的地方需要什么吗?

谢谢!

3 个答案:

答案 0 :(得分:3)

的内容
int i = 0;
while ((line = br.readLine()) != null)
{
   i++;
   if (i % 4 == 0)
   {
      // if i is divisible by 4, then
      // your actual code will get executed
      ...
   }

}

答案 1 :(得分:0)

在循环结束时调用br.readLine()3次,丢弃输出:

BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;

while ((line = br.readLine()) != null)
{
    ...
    for(int i=0;i<3;i++){ br.readLine(); }
}

br.close();

答案 2 :(得分:0)

BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;
int count 0;
while ((line = br.readLine()) != null)
{
    if (count!=3)
        count++;
    else {
        // Do something?
        count=0;
    }

}

br.close();