这是我的 .txt 文件的样子(但每行超过 100 个元素和超过 100 行):
-0.89094 -0.86099 -0.82438 -0.78214 -0.73573 -0.68691 -0.63754
-0.42469 -0.3924 -0.36389 -0.33906 -0.31795 -0.30056 -0.28692
我想要做的是阅读这个 .txt 文件并将它们存储在 Arryalist 中。问题是当我读取和存储这些数据时,他们将所有这些数据放在同一个数组中(我希望它们存储在由一行分割的 2 个数组中)。
这是我的代码:
public class ReadStore {
public static void main(String[] args) throws IOException {
Scanner inFile = new Scanner(new File("Untitled.txt")).useDelimiter("\\s");
ArrayList<Float> temps = new ArrayList<Float>();
while (inFile.hasNextFloat()) {
// find next line
float token = inFile.nextFloat();
temps.add(token);
}
inFile1.close();
Float [] tempsArray = temps.toArray(new Float[0]);
for (Float s : tempsArray) {
System.out.println(s);
}
}
有什么建议可以使这个工作吗?