如何从ifs文本文件(Java)中提取2D双数组
affine 2
0.62367 -0.40337 0.40337 0.62367 0.00 0.00 0.75
-0.37633 -0.40337 0.40337 -0.37633 1.00 0.00 0.25
scale 500
height 690
width 410
xOffset 134
yOffset 112
name Golden Dragon
从这个文本文件中,我想提取一个宽度为2的名为affine的数组。 下面两行中用空格分隔的值是数组中的值
我已经能够从文本文件中读取其他变量,但似乎无法理解数组
以下是我目前的代码:
public FileIfs(File path){
try (BufferedReader ifsReader = new BufferedReader(new FileReader(path))) {
String line = null;
int i = 0; int j = 0;
while((line = ifsReader.readLine()) != null) {
if (line.startsWith("name")) {
name = line.substring(4).trim();
System.out.println("Name: " + name);
}
if (line.startsWith("scale")) {
scale = Double.parseDouble(line.substring(5).trim());
System.out.println("Scale: " + scale);
}
if (line.startsWith("height")) {
height = Integer.parseInt(line.substring(6).trim());
System.out.println("Height: " + height);
}
if (line.startsWith("width")) {
width = Integer.parseInt(line.substring(5).trim());
System.out.println("Width: " + width);
}
if (line.startsWith("xOffset")) {
xOffset = Integer.parseInt(line.substring(7).trim());
System.out.println("xOffset: " + xOffset);
}
if (line.startsWith("yOffset")) {
yOffset = Integer.parseInt(line.substring(7).trim());
System.out.println("yOffset: " + yOffset);
}
if (line.startsWith("affine")) {
int arrLeng = Integer.parseInt(line.substring(6).trim());
System.out.println("Array Length: " + arrLeng);
affine = new double[arrLeng][7];
}
else {
if (line.startsWith(" ")) {
line.trim();
}
String currentLine [] = line.split("\\s+");
if (!line.trim().isEmpty()){
for (String s : currentLine) {
if (!s.trim().isEmpty()) {
affine[i][j++] = Double.parseDouble(s);
}
}
line = ifsReader.readLine();
i++;
j = 0;
}
} //end of ifelse
} //loop through every line of file
ifsReader.close();
}
catch (Exception e) {
System.out.println("could not find file");
} //end of try-catch
}
else部分中的代码是我试图读取数组的地方
如果有人能帮上忙,或者给我指出正确的方向,那就太好了
谢谢
# 1 楼答案
一个可能的解决办法。它使用列表来持久化数组数据。如果需要,可以很容易地将列表转换为实际数组
输出: