How to read a tab separated or tab delimited file in Java program and print the content to console

In a tab delimited file every line or record has the same number of fields that are separated by the tab character.

  • In the following java program,
  • We read the records or lines in a loop
  • For each record, we split the line on the tab character forming an array.
  • Then we format the fields according to the data type (e..g String, int, double etc)
  • Finally we print the data thus read from file to console.

Here is how the program looks like:

package com.icodejava.io;

/**
 * @Author Kushal Paudyal
 * www.sanjaal.com/java
 * www.icodejava.com
 * Last Modfied On: 2010-07-30
 * 
 * TabSeparatedFileReader.java
 * This class reads tab separated values (4 columns) from a file
 * and prints the values to the console.
 */
import java.io.BufferedReader;
import java.io.FileReader;

public class TabSeparatedFileReader {

	public static void main(String args[]) throws Exception {
		/**
		 * Source file to read data from.
		 */
		String dataFileName = "C:/temp/myTabSeparatedFile.txt";

		/**
		 * Creating a buffered reader to read the file
		 */
		BufferedReader bReader = new BufferedReader(
				new FileReader(dataFileName));

		String line;

		/**
		 * Looping the read block until all lines in the file are read.
		 */
		while ((line = bReader.readLine()) != null) {

			/**
			 * Splitting the content of tabbed separated line
			 */
			String datavalue[] = line.split("t");
			String value1 = datavalue[0];
			String value2 = datavalue[1];
			int value3 = Integer.parseInt(datavalue[2]);
			double value4 = Double.parseDouble(datavalue[3]);

			/**
			 * Printing the value read from file to the console
			 */
			System.out.println(value1 + "t" + value2 + "t" + value3 + "t"
					+ value4);
		}
		bReader.close();
	}

	/*
	 * SANJAAL CORPS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
	 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
	 * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
	 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SANJAAL CORPS SHALL NOT BE
	 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
	 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
	 * 
	 * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
	 * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
	 * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
	 * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
	 * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
	 * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
	 * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SANJAAL CORPS
	 * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
	 * HIGH RISK ACTIVITIES.
	 */

}

  1. How to read / write UTF8 and Non-UTF8 files in Java
  2. How To Print A Text File In Java
  3. How to read a tab separated or tab delimited file in Java program and print the content to console
  4. Computing the total, free and usable disk space easily using JDK 1.6
  5. Reading the Resource or Property files in Java as a file stream and outputting the content to console
  6. File Copy From Local Folder or Network Folder Using Java With Capability To Limit The Number
  7. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  8. Reading / Writing File in Java and String Manipulation
  9. How To Read DOC file Using Java and Apache POI
  10. Writing To Excel File Using Apache POI
  11. Reading Excel File Using Java And Apache POI
  12. Calculating Folder Size In Java
  13. How To Rename A File In Java
  14. Calculating Folder Size Graphically and Generating Directory Size Chart In Java
Tagged , , , , , , , . Bookmark the permalink.

4 Responses to Calculating Folder Size Graphically and Generating Directory Size Chart In Java

  1. nocdib says:

    Thank you for this! It is just what I need

  2. d says:

    would that help you tocreate the file from ubunt server

  3. komal gaikwad says:

    in this code i got problem of array out of bound plz tell me solution.

  4. komal gaikwad says:

    In this code i got error
    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1
    plz send me proper code….

Leave a Reply

Your email address will not be published. Required fields are marked *