使用java解析文本文件,需要使用哪一个的建议 [英] Text file parsing using java, suggestions needed on which one to use

查看:61
本文介绍了使用java解析文本文件,需要使用哪一个的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 InputFileStream 和 Scanner 类成功读取文本文件.这很容易,但我需要做一些比这更复杂的事情.首先是关于我的项目的一些背景知识.我有一个带传感器的设备,我正在使用记录器,它将每 10 秒将来自传感器的数据记录到一个文本文件中.每 10 秒它就有一行新的数据.所以我想要的是当我读取文件时将每个单独的传感器数据抓取到一个数组中.例如:速度高度纬度经度

I can successfully read text file using InputFileStream and Scanner classes. It's very easy but I need to do something more complex than that. A little background about my project first.. I have a device with sensors, and I'm using logger that will log every 10sec data from sensors to a text file. Every 10 sec its a new line of data. So what I want is when I read a file is to grab each separate sensor data into an array. For example: velocity altitude latitude longitude

22 250 46.123245 122.539283

22 250 46.123245 122.539283

25 252 46.123422 122.534223

25 252 46.123422 122.534223

所以我需要将高度数据 (250, 252) 抓取到一个数组 alt[] 中;等等 vel[], lat[], long[]...

So I need to grab altitude data (250, 252) into an array alt[]; and so forth vel[], lat[], long[]...

然后文本文件的最后一行将不同的信息,只是一行.它将包含日期、行驶距离、经过时间..

Then the last line of the text file will different info, just a single line. It will have the date, distance travelled, timeElapsed..

所以在做了一些研究之后,我遇到了 InputStream、Reader、StreamTokenizer 和 Scanner 类.我的问题是您会为我的案例推荐哪一种?在我的情况下,是否可以做我需要做的事情?以及它是否能够检查文件的最后一行是什么,以便它可以获取日期、距离等.谢谢!

So after doing a little research I came across InputStream, Reader, StreamTokenizer and Scanner class. My question is which one would you recommend for my case? Is it possible to do what I need to do in my case? and will it be able to check what the last line of the file is so it can grab the date, distance and etc.. Thank you!

推荐答案

我会使用 Scanner.看看例子 此处.另一种选择是使用 BufferedReader 读取一行,然后使用 parse 方法将该行解析为您想要的标记.

I would use Scanner. Take a look at the examples here. Another option for you to use BufferedReader to read a line and then have parse method to parse that line into the tokens you want.

你也可能会发现这个 线程 很有用.

Also you might find this thread to be useful.

基于上面链接的非常快速的代码.输入数组包含您的文件数据标记.

Very quick code base on the link above. The inputs array has your file data tokens.

public static void main(String[] args) {
    BufferedReader in=null;
    List<Integer> velocityList = new ArrayList<Integer>(); 
    List<Integer> altitudeList = new ArrayList<Integer>();
    List<Double> latitudeList = new ArrayList<Double>();
    List<Double> longitudeList = new ArrayList<Double>(); 
    try {
        File file = new File("D:\\test.txt");
        FileReader reader = new FileReader(file);
        in = new BufferedReader(reader);
        String string;
        String [] inputs;
        while ((string = in.readLine()) != null) {
            inputs = string.split("\\s");
            //here is where we copy the data from the file to the data stucture
            if(inputs!=null && inputs.length==4){
                velocityList.add(Integer.parseInt(inputs[0]));
                altitudeList.add(Integer.parseInt(inputs[1]));
                latitudeList.add(Double.parseDouble(inputs[2]));
                longitudeList.add(Double.parseDouble(inputs[3]));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally{
        try {
            if(in!=null){
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //here are the arrays you want!!!
    Integer [] velocities = (Integer[]) velocityList.toArray();
    Integer [] altitiudes = (Integer[]) altitudeList.toArray();
    Double [] longitudes = (Double[]) longitudeList.toArray();
    Double [] latitudes = (Double[]) latitudeList.toArray();
}

这篇关于使用java解析文本文件,需要使用哪一个的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆