我需要遍历一个名为archive.txt的.txt文档,直到该文档中的所有数据都被耗尽。
我尝试在一个多维数组中返回数据,该数组为每八个数据点创建一个新行。
到目前为止,我已经成功地遍历了数据,但似乎无法组织它。
下面的函数只能输出每行的数据行。
private void findContract() {
Scanner input = null; // this is to keep the compiler happy
// as the object initialisation is in a separate block
try {
input = new Scanner(new File("archive.txt"));
} catch (FileNotFoundException e) {
System.out.println("File doesn't exist");
System.exit(1);
}
while (input.hasNext()) {
String dDate = input.next();
System.out.println(dDate);
}
input.close();
}
来自文本文件(archive.txt)的前8个数据点的示例
15-Sep-2015 2 1 12 N MT230N 617 CMcgee
所有这一切的结果是,我需要能够逐行和逐列选择数据点。
如果有人能告诉我正确的方法,我将不胜感激。我尝试过几种方法&上面的函数是显示文件中数据的最后一个实例。
发布于 2016-01-18 01:55:36
在java8中,您可以使用它作为您可能使用的任何分隔符。
public static String[][] fileToMatriz(String file, String delimiter) throws IOException {
try (Stream<String> stream = Files.lines(Paths.get(file))) {
return stream.map(s -> s.split(delimiter))
.toArray(String[][]::new);
}
}
https://stackoverflow.com/questions/34845799
复制相似问题