我从用“;”分隔的 .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;
}
宝慕林4294392
一只名叫tom的猫
相关分类