Parse CSV File using StringTokenizer example

  1. /*
  2. Parse CSV File using StringTokenizer example.
  3. This example shows how to parse comma separated file (CSV file) using
  4. Java StringTokenizer and BufferedReader classes.
  5. */
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.FileReader;
  9. import java.io.FileWriter;
  10. import java.util.StringTokenizer;
  11.  
  12. public class ParseCSVFileExample {
  13.  
  14. public static void main(String[] args) {
  15.  
  16. try
  17. {
  18.  
  19. //csv file containing data
  20. String strFile = "C:/FileIO/example.csv";
  21.  
  22. //create BufferedReader to read csv file
  23. BufferedReader br = new BufferedReader( new FileReader(strFile));
  24. String strLine = "";
  25. StringTokenizer st = null;
  26. int lineNumber = 0, tokenNumber = 0;
  27.  
  28. //read comma separated file line by line
  29. while( (strLine = br.readLine()) != null)
  30. {
  31. lineNumber++;
  32.  
  33. //break comma separated line using ","
  34. st = new StringTokenizer(strLine, ",");
  35.  
  36. while(st.hasMoreTokens())
  37. {
  38. //display csv values
  39. tokenNumber++;
  40. System.out.println("Line # " + lineNumber +
  41. ", Token # " + tokenNumber
  42. + ", Token : "+ st.nextToken());
  43. }
  44.  
  45. //reset token number
  46. tokenNumber = 0;
  47.  
  48. }
  49.  
  50.  
  51. }
  52. catch(Exception e)
  53. {
  54. System.out.println("Exception while reading csv file: " + e);
  55. }
  56. }
  57. }
  58.  
  59. /*
  60. Input csv file
  61. "one","two","three","four"
  62. "parsing","comma","separated","file","java","example"
  63. */
  64.  
  65. /*
  66. Output would be,
  67. Line # 1, Token # 1, Token : "one"
  68. Line # 1, Token # 2, Token : "two"
  69. Line # 1, Token # 3, Token : "three"
  70. Line # 1, Token # 4, Token : "four"
  71. Line # 2, Token # 1, Token : "parsing"
  72. Line # 2, Token # 2, Token : "comma"
  73. Line # 2, Token # 3, Token : "separated"
  74. Line # 2, Token # 4, Token : "file"
  75. Line # 2, Token # 5, Token : "java"
  76. Line # 2, Token # 6, Token : "example"
  77. */

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!