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

best way to read large csv file in java
how to read a particular column from csv file in java
read text file separated tab java
how to read comma separated values from text file in java
java read space separated file
java tab delimiter
java read text file line by line with delimiter

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.

You need to use nextLine instead of next to read the whole line. Also I ran your program and found out that your file is not truely splitted by tab that's why your split may not work. Fix these two things and then you are good to go.

How to read a tab separated or tab delimited file in Java program and, Last Modfied On: 2010-07-30. *. * TabSeparatedFileReader.java. * This class reads tab separated values (4 columns) from a file. * and prints  I have included a simple Java function to print data to a tab delimited file with the extension csv for the convenience of opening the file with Microsoft Excel. I found this very helpful in my last project and hope it will help someone else :).

Here's a sample method which achieves what you're looking for (not tested, but the concept is there). Essentially, you need to read by line, split the line into some sort of array or list, and then have 2d array. you can also replace split("\t") with splitting by white space in general.

public List<String> getByColumn(int col, File file)
{
    List<ArrayList<String>> arrayOfArrays = null;
    try {
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
        BufferedReader br = new BufferedReader(isr);

        String line;

        arrayOfArrays = new ArrayList<ArrayList<String>>();

        while ( ( line = br.readLine() ) != null )
        {
            ArrayList<String> list = new ArrayList<String>(Arrays.asList(line.split("\t")));

            arrayOfArrays.add(list);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    ArrayList<String> output = new ArrayList<String>();

    //can use a foreach loop, or the below method.
    //for (ArrayList<String> l : arrayOfArrays)
    //{
    //    output.add(l.get(col));
    //}

    for ( int i = 1; i < arrayOfArrays.size(); i++ )
    {
        output.add(arrayOfArrays.get(i).get(col));
    }

    return output;

}

How to read tab separated csv file in java, Select a file that has variable names in the first row and values separated by tabs in some rows have There are several ways to read a plain text file in Java e. Number of columns in the file: 3 (probably a constant within your program, or a hardcoded value) Number of values read: 2 (stored in the variable 'count') Now, all what's left to do is: subtract the number of values read from the total number of columns in the file: 3 - 2 = 1 (store this value in a variable called 'startIdx' for example).

You should really use univocity-parsers for that - it will be way faster than your String.split and also helps a lot selecting the columns you want:

//configure the parser
TsvParserSettings parserSettings = new TsvParserSettings();
parserSettings.selectFields("TMAX" /*and others*/);

//then parse
TsvParser parser = new TsvParser(parserSettings);

List<String[]> parsedRows = parser.parseAll(new File("daily.txt"), "UTF-8");

Hope this helps.

Disclaimer: I'm the author of this library. It's open source and free (Apache 2.0 license)

How to Read CSV File in Java, How to Read CSV File in Java with oops, string, exceptions, multithreading, Provide the file name CSVDemo and select CSV (Comma delimited) from the save as Step 2: Write some data into file separated by comma (,). The Scanner class provides a constructor that produces values scanned from the specified file. It's a good practice to set the character set when you read a text file. The used FileReader always uses the default charset which could vary from system to system. Consider using InputStreamReader and FileInputStream as the documentation of FileReader says .

use this code to parse the line

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

2 Ways to Parse CSV Files in Java - BufferedReader , There are many ways to parse or read CSV files in Java, and if you need to and writes files in variations of the Comma Separated Value (CSV) format. MYSQL to parse default MySQL format used by the SELECT INTO TDF to parse tab-​delimited format, with quote; leading and trailing spaces ignored. You can retrieve values using either the index number of the column or the alias or name of the column. The column index is usually more efficient. Columns are numbered from 1. For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once.

Reading a CSV file in Java using OpenCSV, Select a Category A Comma-Separated Values (CSV) file is just a normal plain​-text file, store data in column by column, and split it by a Some useful classes of opencsv Read data line by line : Lets see how to read CSV file line by line. READING FROM A FILE. We will write a java program to read from file and print file data on the user screen. Let’s understand way to associate a File object with the input stream: You can pass the filename to the constructor of the FileInputStream class. You can create a File object by passing the filename to the File constructor.

Tab Separated Values (TSV): a format for tabular data exchange, The Tab Separated Values has been officially registered as an done - instead, the format is programmatically written and read. But it's usually recommendable to use some labeling there. When you have Excel in use, you can open the the File menu, then select Open, and pick up the file to be opened  Somehow I can never remember the cut command in Unix. But I occasionally want to remove certain columns from a text file of data. cut will do that. Delimited. Here is a simple tab-delimited example. (Use the -d option to set a different column delimiter.) Data File (tab-delimited) 111 222 3333 444 555 666

Reading and Writing CSVs in Java, Use FileReader to open the CSV file; Create a BufferedReader and read the For example, another common delimeter is the tab for Tab-Separated-Value (TSV​) files. There are several ways to read and write CSV files in Java, the simplest​  I have String1, String2, String3, String4 and i have to create a file like this. string1 tab string2 tab string 3 tab string4 carriagereturn how can i create the file like this. To be more specific i am reading 6 columns from a table and creating a file with tab delimition between the values and carriage return for each row.

Comments
  • nextLine, not next.
  • And aren't Java arrays 0-based? That would make TMAX at index 3, not 4.
  • Both comments above are correct. I would add that inputStream is a misleading name for a Scanner, which is not an InputStream
  • Is there any benefit to using BufferedReader rather than a Scanner? and why create your arrayOfArrays when the OP only wanted the forth column
  • No real reason to write a method to get just the 4th column when theres a good chance that he might want additional columns in the future. Also, the buffered reader is probably unnecessary here, but it's better convention (and far better performance) when reading from large files.
  • I tried it but there is something wrong with parserSettings.selectFields("TMAX"); .Multiple markers at this line - Syntax error on token ".", @ expected after this token - Syntax error, insert ")" to complete MethodDeclaration - Syntax error, insert "Identifier (" to complete MethodHeaderName - Syntax error, insert "SimpleName" to complete QualifiedName - Syntax error, insert ")" to complete SingleMemberAnnotation
  • I could not understand where the problem exactly is.
  • I'm sorry but all these errors you are getting are basic Java syntax errors which you need to become familiar with. I can't help you a lot there but it sounds like you didn't import the library.