异常消息如何超越已执行的打印行

How can exception message overtake an executed printed line

我从用“;”分隔的 .csv 文件中读取字段分号。我写了例外来处理可能的偏差。但是如果出现异常,NetBeans 会在最后一行之前读出错误消息。

这是输出的样子:

我不明白代码中后面一行怎么可能打印出来。这是我的全部代码:

public static void reader(String fileName) throws IOException {
    try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
        String line;
        List<String> lineHolder = new ArrayList<>();
        StringTokenizer divider;
        String formatter = "%2s|%-30s|%-30s|%10s|%n";

        String separator = "----------";
        System.out.format("ID|%-30s|%-30s|birth date|%n", "name", "email");
        System.out.print("--+" + separator + separator + separator 
                + "+" + separator + separator + separator + "+" 
                + separator + "+\n");

        while ((line = br.readLine()) != null){
            if (line.startsWith("#", 0))
                continue;
            if (!line.contains(";")) {
                throw new IOException("too less data or not proper delimiter");
            }
            divider = new StringTokenizer(line, ";");
            lineHolder = arrayFiller(divider);
            dataChecker(lineHolder, line);
            System.out.format(formatter, lineHolder.get(0), lineHolder.get(1)
                , lineHolder.get(2), lineHolder.get(3));
        }
    } catch (FileNotFoundException ex) {
        System.err.println("The file not found.");
    } catch (IOException ex){
        System.err.println(ex.getMessage());
    }
    System.out.print("\n");
}

public static ArrayList<String> arrayFiller(StringTokenizer divider) {
    ArrayList<String> lineHolder = new ArrayList<>();
    while (divider.hasMoreTokens()) {
        lineHolder.add(divider.nextToken());
    }
    return lineHolder;
}

这些是例外情况:

public static void dataChecker(List<String> lineHolder, String line) throws IOException {
    if (lineHolder.size() < 4) {
        throw new IOException("too less data or not proper delimiter");
    } else if (lineHolder.size() > 4) {
        throw new IOException("too much data");
    } else if (lineHolder.get(0).length() > 2 
            || !Pattern.matches("[0-9]+", lineHolder.get(0))) {
        throw new IOException("Error during reading the file: "
                + "not proper ID field format");
    } else if (lineHolder.get(1).length() > 30 
            || !Pattern.matches("[a-zA-ZíÍöÖüÜóÓőŐúÚűŰáÁéÉ. ]+", lineHolder.get(1))) {
        throw new IOException("Error during reading the file: "
                + "not proper Name field format");
    } else if (lineHolder.get(2).length() > 30 
            || !Pattern.matches("[a-zA-Z0-9@. ]+", lineHolder.get(2))) {
        throw new IOException("Error during reading the file: "
                + "not proper Email field format");
    } else if (lineHolder.get(3).length() > 10 || dateFormatter(lineHolder.get(3))) {
        throw new IOException("Error during reading the file: "
                + "not proper Birth date field format");
    } 
}

public static boolean dateFormatter(String datum) {
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    try {
        LocalDate changedDate = LocalDate.parse(datum, dtf);
        return false;
    } catch (DateTimeParseException ex) {
        return true;
    }
}

和源文件:

#ID;name;email;birth date
1,Jakob George,gipszjakab@gmail.com,1981-11-23
2;Susan Smith;usa@gmail.com;1982-12-01
3;Poul Drake;gameover@gmail.com;1990-01-02
4;Isaac Wheather;ruck%sack@freemail.hu;1988-01-22
5;George T. Benson;bigman@hotmail.com;1977-08-12

我试图将方法(异常持有者)放入 reader() 方法中,但结果相同。这怎么可能,我做错了什么?

错误消息通过不同的输出流打印。标准输出流 stdout 用于正常 logging/output,错误(通过 System.err.println)转到 stderr。您的 console/terminal 显示两者,但他们不会等待对方完成打印内容。

编辑:也许 this 也有帮助。

EDIT2:如果您将错误输出改为打印到文件,您将在 console/terminal 中丢失错误输出。但也许这对你来说没问题?像这样:

//set err out to print to file
PrintStream ps = new PrintStream("err.log");
System.setErr(ps);

//cause exception for testing it
String s = null;
s.length();

如果您想要 错误和标准输出打印到 console/terminal,则无法控制两个流的时间,因为每行打印一个独立的操作。

    String separator = "----------";
    System.out.format("ID|%-30s|%-30s|birth date|%n", "name", "email");
    System.out.print("--+" + separator + separator + separator 
            + "+" + separator + separator + separator + "+" 
            + separator + "+\n");
    System.out.flush();
    while ((line = br.readLine()) != null)

由于 System.out 和 System.err 来自不同的输出流,因此它们不会等待彼此完成。打印语句后立即清除缓冲区内存即可。