使用bufferedreader在第一次读取时缺少行

时间:2016-11-29 22:11:57

标签: java parsing bufferedreader

我有以下算法来读取整个文本文件,该文件将每个标题下的信息解析为相应的列表

    BufferedReader br;
    try {
        String c;
        br = new BufferedReader(new FileReader((TRAFFIC_FILE)));
        while((c = br.readLine()) != null) {

            //read the 1st part of the file till 2nd header line
            List<String> details = readLines(br,
                    "============= R ===================");

            //read the 2nd part of the file till 3rd header line
            List<String> request = readLines(br,
                    "============= R ===================");

            //read the 3rd part of the file till 4th header line
            List<String> response = readLines(br,
                    "============= D ===================");

            System.out.println("=D=");
            System.out.println(c);
            System.out.println(d + "\n");
            System.out.println("=R=");
            System.out.println(r + "\n");
            System.out.println("=R=");
            System.out.println(r + "\n");

        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //close BufferedReader
    }
}

private static List<String> readLines(BufferedReader br, String nextHeader) throws IOException {
    String sCurrentLine;
    List<String> lines = new ArrayList<>();
    while ((sCurrentLine = br.readLine()) != null) {
        if (sCurrentLine.equals(nextHeader) || (nextHeader != null && nextHeader.equals(sCurrentLine))) {
            break;
        }
        if (sCurrentLine.isEmpty()){
           sCurrentLine = "";
        }
        else {
            lines.add(sCurrentLine.trim());
        }
    }
    return lines;
    }
  }

上述工作,但我必须为第一个读取行分配一个变量,因为读者在首次读取后会对其进行处理。是否有任何可能的方法来阅读第一行而不讨论它?只是这样我可以保留第一行及其相应的列表,而不是稍后追加?例如,因为我目前打印出缺失的行

3 个答案:

答案 0 :(得分:1)

您只需删除while内的main()循环,然后将br对象(BufferedReader)传递给readLines,如下所示:

public static void main(String[] args) {
        BufferedReader br;
        try {
            br = new BufferedReader(new FileReader((TRAFFIC_FILE)));

            //read the 1st part of the file till Request header line
            List<String> details = readLines(br,
                    "============= Request ===================");

            //read the 2nd part of the file till Response header line
            List<String> request = readLines(br,
                    "============= Response ===================");

            //read the 3rd part of the file till Details header line
            List<String> response = readLines(br,
                    "============= Details ===================");

            System.out.println("=Details=");
            System.out.println(c);
            System.out.println(details + "\n");
            System.out.println("=Request=");
            System.out.println(request + "\n");
            System.out.println("=Response=");
            System.out.println(response + "\n");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        //close BufferedReader
        }
    }

答案 1 :(得分:0)

试试吧

function test
disp('Hello!');

答案 2 :(得分:-1)

您可以使用Scanner代替BufferedReader。这样你可以检查是否有更多行可用而不读它们。

以下是一个例子:

try(Scanner sc = new Scanner(new File(TRAFFIC_FILE))){
    while(sc.hasNextLine()){
        List<String> details = readLines(sc, "============= Request ===================");
        ...
        ...
    }
}catch(Exception e){
    e.printStackTrace();
}


private static List<String> readLines(Scanner sc, String nextHeader) throws IOException {
    List<String> lines = new ArrayList<>();
    while (sc.hasNextLine()) {
        String sCurrentLine = sc.nextLine();
        if (nextHeader.equals(sCurrentLine)) {
            break;
        }else{
            lines.add(sCurrentLine.trim());
        }
    }
    return lines;
}

这种方法只有在文件包含多个请求,响应和详细信息标头时才有意义,如果它只包含其中一个,则@javaguy方法更合适。