gpt4 book ai didi

java - 如何在循环中选取 ASCII 输入文件中的列

转载 作者:行者123 更新时间:2023-11-30 03:15:23 24 4
gpt4 key购买 nike

我有一个大约 160 行的 ASCII 文件(假设为 result.txt),其中包含以下内容:

Just a comment
with two or more lines

File title, Versionnumber

Time Value1 Value2 Value3 Value4
(s) (m) (m/s) (km) (N)
0.002 8.000E+00 0.000E+00 0.000E+00 10.000E+01
0.040 9.850E+00 1.221E-04 0.000E+00 12.000E+01
0.060 12.780E+00 1.312E-02 0.000E+00 16.000E+05
...

我现在想根据时间列处理每个值列。这个想法是声明一个映射并将每个时间步的值放入其中。因此 Value1 的第一个 map 将如下所示:

0.002 -> 8.000E+00
0.040 -> 9.850E+00
0.060 -> 12.780E+00

Value2Value3、...、ValueN 的映射看起来与上面显示的分配类似。时间步长可以是 double 值,而值可以是字符串(我知道如何根据需要转换它们)。

最简单、最快且最有效的方法是什么?

更新:这是我读取文件的方式:

FileInputStream fis;
try {
fis = new FileInputStream("file.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));

String line;
while ((line = br.readLine()) != null) {
// split columns
System.out.println(line);
}
br.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}

拆分列的最佳方法是什么?

最佳答案

以下代码可用于填充已有的框架:

// class level declaration
Map<Double, String> timeSeriesMap = new HashMap<Double, String>();

while ((line = br.readLine()) != null) {
// split current line on any amount of whitespace
String[] parts = line.trim().split("\\s+");
Double key = Double.valueOf(parts[0]);
String value = parts[1];

// add the (key, value) pair to your Map
timeSeriesMap.put(key, value);

System.out.println(line);
}

关于java - 如何在循环中选取 ASCII 输入文件中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32776525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com