How to read a tab separated file and select few values from it using java

Viewed 6173

I have a tab separated file which looks like this

 STID  STNM  TIME     TMAX TMAXO     TMIN TMINO    TAVG     TBAD     DMAX DMAXO     DMIN DMINO    DAVG    VDEF     DBAD     SMAX SMAXO     SMIN SMINO    SAVG     SBAD     BMAX BMAXO     BMIN BMINO    BAVG     BBAD     S5MX S5MXO     S5MN S5MNO    S5AV     S5BD     S25X S25XO     S25N S25NO   S25AV    S25BD     S60X S60XO     S60N S60NO   S60AV    S60BD     HMAX HMAXO     HMIN HMINO    HAVG     HBAD     PMAX PMAXO     PMIN PMINO    PAVG    MSLP     PBAD     AMAX AMAXO     ATOT     ABAD     PDIR     PDFQ     SDIR     SDFQ     IBAD     WSMX WSMXO     WSMN WSMNO    WSPD    WDEV    WMAX WMAXO     WBAD     RAIN     RNUM    RMAX     RBAD    9AVG     9BAD     2MAX     2MIN     2AVG     2DEV     2BAD     HDEG     CDEG     HTMX HTMXO    HTBAD     WCMN WCMNO    WCBAD
 ACME   110     0    76.32   131    69.22   184   71.57        0    69.10   286    61.55     3   66.48    4.22        0    83.16     3    78.24   288   80.85        0    85.37     3    77.74   288   81.77        0    83.12   150    77.86   288   80.58        0    83.84     3    81.23   288   82.34        0    81.54     3    80.94   285   81.29        0    96.82   278    66.82     1   84.59        0    28.74   284    28.67    23   28.71   30.10        0   412.73   130     5.46        0     -996     -999     -996     -999       59    10.92   132     0.00    37    4.34    2.41   14.61   146        0     0.22       19    0.24        0   71.67        0     8.44     0.00     2.49     2.30        0     0.00     7.77     -996   999      288     -996   999      288
 ADAX     1     0    73.99    96    68.61    21   71.32        0    70.91   169    62.77     1   68.22    2.58        0    87.15     3    82.99   288   84.83        0    88.32     3    79.54   288   83.59        0    85.06     3    81.84   288   83.31        0    88.48     3    85.21   288   86.61        0     -996   999     -996   999    -996       96    98.40   274    73.27     1   90.20        0    29.08   137    29.01    17   29.04   30.08        0   210.42   151     5.23        0     -996     -999     -996     -999      139    12.83   106     0.00    33    3.65    3.03   19.28   121        0     0.24       23    0.24        0   71.57        0     8.84     0.00     2.07     2.48        0     0.00     6.30     -996   999      288     -996   999      288
 ALTU     2     0    75.51   107    68.74   168   71.63        0    70.43   279    64.56   125   67.48    3.50        0    80.60     3    77.88   288   78.91        0    79.11     3    75.96   288   77.08        0    79.97     3    77.23   288   78.41        0    81.95     3    79.57   288   80.55        0     -996   999     -996   999    -996       96    98.36   286    70.28   106   87.18        0    28.68   276    28.60    51   28.64   30.09        0   202.20   123     5.03        0        2    30.80        4    18.63       25    13.72   128     0.00    70    5.79    2.71   18.19   128        0     0.19       19    0.12        0   71.53        0     9.55     0.00     3.71     2.22        0     0.00     7.12     -996   999      288     -996   999      288

I am trying to read this file so that I can append some of the values from this file to another file.

But firstly I am unable to read the values of the column TMAX which is 4th in the columns

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

    public class first {

        public static void main(String[] args) 
        {
            // TODO Auto-generated method stub

                        String fileName="daily.txt";
                    File file = new File(fileName);
                    try{
                        Scanner inputStream = new Scanner(file);
                        while (inputStream.hasNext()){
                            String data = inputStream.next();
                            String[] values = data.split("\t");
                            System.out.println(values[4]);
                        }
                        inputStream.close();
                        }
                    catch(FileNotFoundException e){
                        e.printStackTrace();
                    }
                }
    }

When I use the above code the output looks like this

STID
STNM
TIME
TMAX
TMAXO
TMIN
TMINO
TAVG
TBAD
DMAX
DMAXO
DMIN
DMINO
DAVG
VDEF
DBAD
SMAX
SMAXO
SMIN

I want to get an output which displays the values of the specified column numbers.

4 Answers

use this code to parse the line

String[] values = data.trim().replaceAll(" +", " ").split(" ");
Related