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

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

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

你正在读两行……

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

你有错误:

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

你应该保持read = in.readLine()。 并删除另一行。

请试试这个

你使用“read = in.readLine())”在while循环中两次,为什么它会滑动lomes

 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) { 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;