我正试图找出如何从如下格式的文本文件中读取数据:
1.0,8.0
4.0,3.0
6.0,0.0
3.0,5.0
在myNumbers1中添加每行的第一个元素,在myNumbers2中添加第二个元素,如下所示:
double[]myNumbers1={}
double[]myNumbers2={};
要得到
double[]myNumbers1={1.0,4.0,6.0,3.0}<双[]myNumbers2={8.0,3.0,0.0,5.0};
这有意义吗?谢谢!:)
下面是实现你想要的最简单的方法。
List<String> lines = Files.readAllLines(Paths.get("src/main/resources/file.txt"));
double[] first = new double[lines.size()];
double[] second = new double[lines.size()];
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
String[] split = line.split(",");
first[i] = Double.parseDouble(split[0]);
second[i] = Double.parseDouble(split[1]);
}