【问题标题】:BufferedReader is skipping every other line when reading my file in java在java中读取我的文件时,BufferedReader 每隔一行跳过
【发布时间】:2014-10-06 09:56:19
【问题描述】:

所以我正在阅读一个包含我之前在代码中写入的约会的文件。我想筛选文本文件并找到某个日期的约会并将它们添加到 ArrayList 但是当 BufferedReader 通过它时,它会跳过其他行...这是我的代码

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;
}

【问题讨论】:

    标签: java bufferedreader lines


    【解决方案1】:

    您正在阅读该行两次..

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

    【讨论】:

      【解决方案2】:

      这里有错误:

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

      您应该暂时保留 read = in.readLine() 。并删除另一行。

      【讨论】:

        【解决方案3】:

        请试试这个

        你在 while 循环中使用了两次“read = in.readLine())”,这就是为什么它跳过了 lomes

        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) {
        
                        String[] split = read.split(",");
                        System.out.println(read);
        
                        if (split[0].equals(Integer.toString(checkDay)) && split[1].equals(Integer.toString(checkMonth)) && split[2].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;
        

        【讨论】:

        • 答案在哪里?
        猜你喜欢
        • 2019-03-10
        • 2011-11-04
        • 1970-01-01
        • 1970-01-01
        • 2014-01-26
        • 2016-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多