How to Read a Tab Delimited File in Java

Reading tab delimited files is an essential way to read basic output from Java. Some databases export data in tab or comma delimited format, so importing that data in Java is a way to display the results of a database to an application that is unable to query the tables directly. Importing and reading tab delimited files only takes a few lines of code in the application development with Java.

Instructions

    1

    Import the necessary class libraries. Reading files requires the IO class library that is not automatically included. The following syntax includes libraries for reading tab delimited files:
    import java.io.BufferedReader;
    import java.io.FileReader;

    2

    Set the variables to read the files. File buffers are used to read file data into memory for faster processing. The following text reads a file that is tab delimited to parse the information.
    BufferedReader readbuffer = new BufferedReader(new FileReader("myFile.txt"));
    String strRead;

    3

    Read the delimited information. The following loop separates the tab delimited information into an array variable and prints it to the console. The "\t" notation is the switch that indicates a tab delimeter:
    while ((strRead=readbuffer.readLine())!=null)
    String splitarray[] = strRead.split("\t");
    String firstentry = splitarray[0];
    String secondentry = splitarray[1];
    System.out.println(firstentry + " " + secondentry);

    4

    Close the stream to free memory resources on the host computer:
    readbuffer.close();

Blog Archive