java - 如何从 ifs 文本文件中提取 2D double 组 (Java)

标签 java arrays regex bufferedreader

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

我想从这个文本文件中提取一个名为 affine、宽度为 2 的数组。 接下来的两行中用空格分隔的以下值是数组内的值。

我已经能够从文本文件中读取其他变量,但似乎无法找出数组。

这是迄今为止我的代码:

 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 部分中的代码是我尝试读取数组的位置。

如果有人可以提供帮助,或者为我指明正确的方向,那就太好了。

谢谢。

最佳答案

一个可能的解决方案。它使用列表来保存数组数据。如果需要,可以轻松将列表转换为真正的数组。

public void fileIfs(Path path) throws IOException {
    try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) {

        // ------- read parameters -------

        Pattern paramPattern = Pattern.compile("([A-Za-z]+)\\s+([\\w\\s]+)");
        Matcher paramMatcher = paramPattern.matcher(lines.collect(Collectors.joining(",")));
        Map<String, String> params = new HashMap<>();

        while (paramMatcher.find()) {
            params.put(paramMatcher.group(1), paramMatcher.group(2));
        }
        String name = params.get("name");
        int affine = Integer.parseInt(params.get("affine"));
        int scale = Integer.parseInt(params.get("scale"));
        int height = Integer.parseInt(params.get("height"));
        int width = Integer.parseInt(params.get("width"));
        int xOffset = Integer.parseInt(params.get("xOffset"));
        int yOffset = Integer.parseInt(params.get("yOffset"));

        // ------- read array -------

        List<List<Double>> affineList = new ArrayList<>();
        Pattern arrayPattern = Pattern.compile("([\\-\\d]+\\.\\d+)");

        lines.forEach(line -> {
            Matcher arrayMatcher = arrayPattern.matcher(line);
            List<Double> numbers = new ArrayList<>();
            while (arrayMatcher.find()) {
                numbers.add(Double.parseDouble(arrayMatcher.group(1)));
            }
            if (!numbers.isEmpty()) {
                affineList.add(numbers);
            }
        });

        // ---------------

        System.out.println("params: " + params);
        System.out.println("affineList: " + affineList);
    }
}

输出:

params: {yOffset=112, xOffset=134, width=410, name=Golden Dragon, scale=500, affine=2, height=690}
affineList: [[0.62367, -0.40337, 0.40337, 0.62367, 0.0, 0.0, 0.75], [-0.37633, -0.40337, 0.40337, -0.37633, 1.0, 0.0, 0.25]]

关于java - 如何从 ifs 文本文件中提取 2D double 组 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60940481/

相关文章:

javascript - 正则表达式 javascript 删除 _ -

java - 为以下模式生成正则表达式

Java:如何在 JFrame/JPanel 中打开 Google 之类的网页?

java - 对没有连接的实体的条件查询

java - 使用反射将值设置到对象中

C++ - 数组指针作为函数参数,填充数组并从 UI 线程访问它

java - 使用 LostFocus 事件在 Java 中验证文本字段

c - 在数组 : Segmentation fault (core dumped) 中插入一个元素

php - 在 isset() 之后将值添加到数组

Javascript:正则表达式替换两个字符及其周围的所有空格