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

时间:2014-10-06 09:56:19

标签: java bufferedreader lines

所以我正在阅读包含我在我的代码中先前写过的约会的文件。我想筛选文本文件并在某个日期查找约会并将它们添加到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;
}

3 个答案:

答案 0 :(得分:8)

你正在读两行......

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

答案 1 :(得分:2)

这里有错误:

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

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

答案 2 :(得分:1)

试试这个

你使用&#34; read = in.readLine())&#34;在while循环中两次,为什么它正在滑动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;