在Java中读取我的文件时,BufferedReader跳过每隔一行 [英] BufferedReader is skipping every other line when reading my file in java

查看:533
本文介绍了在Java中读取我的文件时,BufferedReader跳过每隔一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此Im正在读取包含我在代码中先前写入的约会的文件.我想在文本文件中筛选并找到某个日期的约会,然后将其添加到ArrayList中,但是当BufferedReader通过它时,它会跳过其他所有行……这是我的代码

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<String> read(int checkDay, int checkMonth, int checkYear) {
    ArrayList<String> events = new ArrayList<String>();
    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;
}

推荐答案

您正在两次阅读该行.

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

这篇关于在Java中读取我的文件时,BufferedReader跳过每隔一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆