我从.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()
方法中,但结果仍然相同。这怎么可能?我做错了什么?
答案 0 :(得分:3)
错误消息是通过不同的输出流打印的。标准输出流NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kafka-service NodePort 10.99.113.234 <none> 32400:32400/TCP 6m34s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 27d
my-confluent-oss-cp-kafka ClusterIP 10.100.156.108 <none> 9092/TCP 102m
my-confluent-oss-cp-kafka-connect ClusterIP 10.99.78.89 <none> 8083/TCP 102m
my-confluent-oss-cp-kafka-headless ClusterIP None <none> 9092/TCP 102m
my-confluent-oss-cp-kafka-rest ClusterIP 10.100.152.109 <none> 8082/TCP 102m
my-confluent-oss-cp-ksql-server ClusterIP 10.96.249.202 <none> 8088/TCP 102m
my-confluent-oss-cp-schema-registry ClusterIP 10.109.27.45 <none> 8081/TCP 102m
my-confluent-oss-cp-zookeeper ClusterIP 10.102.182.90 <none> 2181/TCP 102m
my-confluent-oss-cp-zookeeper-headless ClusterIP None <none> 2888/TCP,3888/TCP 102m
schema-registry-service NodePort 10.103.100.64 <none> 32500:32500/TCP 33m
zookeeper-np NodePort 10.98.180.130 <none> 32181:32181/TCP 53m
用于正常的日志记录/输出,错误(通过System.err.println)转到stdout
。您的控制台/终端会同时显示两者,但它们不会彼此等待完成打印内容。
编辑:也许this也有帮助。
EDIT2:如果您将错误更改为打印到文件,则将丢失控制台/终端中的错误输出。但是,也许对您来说还好吗?像这样:
stderr
如果要在控制台/终端上打印两者错误和标准输出,则由于每条印刷线都是独立的操作,因此无法控制两个流的时序。
答案 1 :(得分:1)
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来自不同的输出流,因此它们不会彼此等待完成。在print语句之后立即清除缓冲区内存即可解决此问题。