StringTokenizer in Java

The StringTokenizer class of java.util package, allows an application to split or break a string into small parts by defined delimiter ( space is the default delimiter). Each part of the splited string is called a token . This is particularly helpful for text processing where you need to split a string into several parts and use each part as an element for individual processing.
StringTokenizer st = new StringTokenizer("Java String Tutorial");
From the javadocs:
  1. StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

The following Java program split the given string with space as delimiter

Example
import java.util.*; class TestClass{ public static void main (String[] args){ //string characters seperated by space String str = "Java String Tutorial"; //space is the default delimiter //so we dont specify any delimiter StringTokenizer st = new StringTokenizer(str); while (st.hasMoreElements()) { System.out.println(st.nextElement()); } } }
Output
Java String Tutorial

StringTokenizer with coma(,) as delimiter

import java.util.*; class TestClass{ public static void main (String[] args){ //string characters seperated by coma(,) delimiter String str = "NORTH,SOUTH,EAST,WEST"; StringTokenizer st = new StringTokenizer(str,","); while (st.hasMoreTokens()) { System.out.println(st.nextElement()); } } }
Output
NORTH SOUTH EAST WEST

How to read and parse CSV file

The following program read a CSV file and split the character with coma(,) delimiter

import java.util.*; import java.io.*; class TestClass { public static void main (String[] args) { BufferedReader bReader = null; try{ String line; bReader = new BufferedReader(new FileReader("d:/sample.csv")); while ((line = bReader.readLine()) != null) { StringTokenizer st = new StringTokenizer(line,","); while (st.hasMoreTokens()) { System.out.println(st.nextElement()); } } }catch(IOException ex1){ ex1.printStackTrace(); }finally { try { if (bReader!= null) bReader.close(); } catch (IOException ex2) { ex2.printStackTrace(); } } } }