Parsing a text file using java, suggestions needed to use

I can successfully read a text file using the InputFileStream and Scanner classes. It is very easy, but I need to do something more complex than this. A little bit about my project at the beginning. I have a device with sensors, and I use a recorder that will record data from sensors every 10 seconds in a text file. Every 10 seconds is a new row of data. So what I want, when I read the file, is to capture each individual sensor into an array. For example: speed altitude latitude longitude

22 250 46.123245 122.539283

25 252 46.123422 122.534223

Therefore, I need to take the height data (250, 252) into the alt [] array; etc. vel [], lat [], long [] ...

Then in the last line of the text file there will be different information, just one line. It will have a date, distance traveled, timeElapsed ..

So, after a little research, I came across the classes InputStream, Reader, StreamTokenizer and Scanner. 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 whether he can check what the last line of the file is, so that he can capture the date, distance, etc. Thank!

+1
source share
3 answers

I would use Scanner. Take a look at the examples here . Another option is to use BufferedReader to read a string, and then the parse method to parse that string in the desired markers.

.

. .

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();
}
0

Reader + String.split()

String line;
String[] values;
BufferedReader reader = new BufferedReader(new FileReader(args[0]));
List<Integer> velocity = new ArrayList<Integer>();
List<Integer> altitude = new ArrayList<Integer>();
List<Float> latitude = new ArrayList<Float>();
List<Float> longitude = new ArrayList<Float>();

while (null != (line = reader.readLine())) {
    values = line.split(" ");
    if (4 == values.length) {
        velocity.add(Integer.parseInt(values[0]));
        altitude.add(Integer.parseInt(values[1]));
        latitude.add(Float.parseFloat(values[2]));
        longitude.add(Float.parseFloat(values[3]));
    } else {
        break;
    }
}

:

velocity.toArray();

, 4 , 3 (, , )

+1

Since your data is relatively simple, BufferedReaderand StringTokenizershould do the trick. You will need to read ahead one line at a time to find out when more lines are left.

Your code might be something like this

      BufferedReader reader = new BufferedReader( new FileReader( "your text file" ) );

      String line = null;
      String previousLine = null;

      while ( ( line = reader.readLine() ) != null ) {
          if ( previousLine != null ) {
             //tokenize and store elements of previousLine
          }
          previousLine = line;
      }
      // last line read will be in previousLine at this point so you can process it separately

But how you process the line yourself, you can use Scannerit if you feel more comfortable with it.

0
source

All Articles