BufferedReader在java中读取我的文件时会每隔一行跳过一次



所以我正在读取一个文件,其中包含我在代码早期写的约会。我想筛选文本文件,找到某个日期的约会,并将其添加到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;
}

您正在读取该行两次。。

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

这里有错误:

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

您应该将read=保留在.readLine()中。并移除另一条线。

pl尝试这个

你在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;

可能是"next()";超过";nextLine()"应该解决问题:

System.out.println("Enter Emp ID: ");
        eid = scanner.nextInt();
        System.out.println("Enter Emp Name:");
        ename = scanner.next();
        System.out.println("Enter Emp Salary:");
        esal = scanner.nextFloat();
        System.out.println("Enter Emp Address:");
        eaddr = scanner.next();

最新更新