看看下面的程序。
try {
for (String data : Files.readAllLines(Paths.get("D:/sample.txt"))){
String[] de = data.split(";");
System.out.println("Length = " + de.length);
}
} catch (IOException e) {
e.printStackTrace();
}
示例.txt:
1;2;3;4 甲;乙;; a;b;c;
输出:
长度 = 4 长度 = 2 长度 = 3
为什么第二个和第三个输出给出 2 和 3 而不是 4。在sample.txt
文件中,第二行和第三行的条件是应该\n
在给第三个字段的分隔符后立即给出换行符(或输入)。谁能帮助我如何在不更改sample.txt
文件条件的情况下将第 2 行和第 3 行的长度设为 4 以及如何打印de[2]
(throws ArrayIndexOutOfBoundsException
) 的值?