简体   繁体   English

如何将.txt文件中的2D浮点矩阵导入Java中的2D浮点数组?

[英]How to import a 2D float matrix from .txt file into a 2D float array in Java?

My data.txt file takes the following format: 我的data.txt文件采用以下格式:

14 -0.0 0.29964766 10.7192135 0.0 0.0 0.0 0.0 0.0 0.0
15 -0.0 0.29964766 10.7192135 0.0 0.0 0.0 -8.4375 -36.6875 2.25
15 -0.0 0.29964766 10.7192135 0.20300001 -0.083000004 -0.019000001 -8.4375 -36.6875 2.25
16 -0.0 0.29964766 10.7192135 0.20300001 -0.083000004 -0.019000001 -8.4375 -36.6875 2.25
16 -0.0 0.29964766 10.7192135 0.136 -0.072000004 -0.020000001 -8.4375 -36.6875 2.25
16 -0.0 0.29964766 10.7192135 0.136 -0.072000004 -0.020000001 -8.4375 -36.6875 2.25
17 -0.0 0.29964766 10.7192135 0.136 -0.072000004 -0.020000001 -8.4375 -36.6875 2.25
20 0.21792556 0.40861043 9.956474 0.136 -0.072000004 -0.020000001 -8.4375 -36.6875 2.25
21 0.21792556 0.40861043 9.956474 0.136 -0.072000004 -0.020000001 -8.4375 -36.375 1.375
...

It always has 16 columns. 它始终有16列。 But the row number is unknown because it is actually the number of data set collected, and thus depend on how long are the data being collected. 但是行号是未知的,因为它实际上是收集的数据集的数目,因此取决于收集数据的时间。

I wish to import these data into a 2D float array in Java so that I can easily take out one column for analysis purpose. 我希望将这些数据导入Java中的2D浮点数组中,以便我可以轻松取出一列用于分析。

Can someone kindly help? 有人可以帮忙吗?


The unknown row number is not an issue. 未知的行号不是问题。 I can easily write out a getRowNumber() function. 我可以轻松地写出getRowNumber()函数。

What really bothers me is how to really import all the numbers in. 真正困扰我的是如何真正导入所有数字。

If you want to know the number of the line in a file you can read it and put a counter in the while loop 如果您想知道文件中的行号,可以读取它,并将计数器放在while循环中

    public int lineCounter(){
     BufferedReader br = new BufferedReader(new FileReader("file.txt"));
     int i =0;   
     try {
            String line = br.readLine();

            while (line != null) {
                i++;
            }
            String everything = sb.toString();
        } finally {
            br.close();
        }
        return i;
    }

Then you can set up your 2d matrix and work it as you want 然后,您可以设置二维矩阵并根据需要进行操作

Hope this help 希望这个帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM