java读取文件跳过,在Java中读取文件时,BufferedReader跳过其他所有行

这篇博客解决了在读取文本文件时遇到的问题,即BufferedReader在遍历文件时跳过了每行的偶数行。作者通过检查代码发现,他们在读取一行后又额外调用了一次`readLine()`,导致了这个问题。解决方案是删除多余的读取行的语句,以正确地逐行处理文件内容。
摘要由CSDN通过智能技术生成

So Im working of reading a file containing appointments that I wrote to earlier in my code. I want to sift through the text file and find appointments on a certain date and add them to an ArrayList but when the BufferedReader goes through it, it skips ever other line... Heres my code

public ArrayList read(int checkDay, int checkMonth, int checkYear) {

ArrayList events = new ArrayList();

BufferedReader in = null;

String read;

try {

in = new BufferedReader(new FileReader("calendar.txt"));

while ((read = in.readLine()) != null) {

read = in.readLine();

String[] split = read.split(",");

System.out.println(read);

if (split[1].equals(Integer.toString(checkDay)) && split[2].equals(Integer.toString(checkMonth)) && split[3].equals(Integer.toString(checkYear))) {

events.add(split[0] + " : " + split[1] + "/" + split[2] + "/" + split[3]);

}

}

} catch (IOException e) {

System.out.println("There was a problem: " + e);

e.printStackTrace();

} finally {

try {

in.close();

} catch (Exception e) {

}

}

return events;

}

解决方案

You are reading the line twice..

while ((read = in.readLine()) != null) { // here

read = in.readLine(); // and here

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值