从文件中过滤数据

发布于 2025-01-04 20:56:49 字数 575 浏览 1 评论 0原文

我在下面的文本文件中有大约 50 行数据,其中包含以下

Date= 1/1/2012 (dd:mm:yyyy) Time= 1:44:10 (hh:mm:ss)
Recording Started at 1:44:10 (hh:mm:ss)
X-Value = -0.525108, Y-Value = 7.746691, Z-Value = 5.863008, Timestamp(milliseconds) = 23001
X-Value = -0.755030, Y-Value = 7.861651, Z-Value = 6.016289, Timestamp(milliseconds) = 23208
X-Value = -0.448467, Y-Value = 8.551417, Z-Value = 4.943320, Timestamp(milliseconds) = 23401
Recording Stopped at 1:44:11 (hh:mm:ss)

内容我现在使用的代码使用 BufferedReader 读取文件的每一行,但我真正想做的是提取 Y 值、Z 值和每行的时间戳(毫秒)值然后将其存储在某种数据结构中?这样做的最好方法是什么?

I've got around 50 lines of data in a text file below containing the following

Date= 1/1/2012 (dd:mm:yyyy) Time= 1:44:10 (hh:mm:ss)
Recording Started at 1:44:10 (hh:mm:ss)
X-Value = -0.525108, Y-Value = 7.746691, Z-Value = 5.863008, Timestamp(milliseconds) = 23001
X-Value = -0.755030, Y-Value = 7.861651, Z-Value = 6.016289, Timestamp(milliseconds) = 23208
X-Value = -0.448467, Y-Value = 8.551417, Z-Value = 4.943320, Timestamp(milliseconds) = 23401
Recording Stopped at 1:44:11 (hh:mm:ss)

The code I have right now uses a BufferedReader reading every line of the file but what I really want to do is extract the Y-Value, Z-Value and Timestamp(milliseconds) values from each line and then store it in some sort of data structure? What would be the best way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一笑百媚生 2025-01-11 20:56:49

使用 String.split(),使用逗号和空格作为分隔符,然后使用=作为分隔符分割结果数组元素。然后,将生成的数组元素添加到 ArrayList< /a>.

String str = "X-Value = -0.525108, Y-Value = 7.746691, Z-Value = 5.863008, Timestamp(milliseconds) = 23001
";
String[] data_array = str.split(", ");    
ArrayList<String> Y_Value = new ArrayList<String>();
Y_Value.add(data_array[1].split("= ")[1]);

Use String.split() with comma and white-spae as a delimiter and than split the resultant array element with = as a delimiter. Than, add the resultant array elements into an ArrayList.

String str = "X-Value = -0.525108, Y-Value = 7.746691, Z-Value = 5.863008, Timestamp(milliseconds) = 23001
";
String[] data_array = str.split(", ");    
ArrayList<String> Y_Value = new ArrayList<String>();
Y_Value.add(data_array[1].split("= ")[1]);
半世蒼涼 2025-01-11 20:56:49

看起来您已经回答了自己的问题 - 使用正则表达式 (模式)从每行提取值并将其存储在适合您情况的数据结构中。看看这里举个例子。这是假设您只需要值本身而不是标识符。

It looks like you've answered your own question - use a regex (Pattern) to extract the values from each line and store it in a data structure that is appropriate for your situation. Take a look here for an example. This is assuming that you want only the values themselves and not the identifiers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文