• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading csv file

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, my requirement is to read from a .csv (comma seprated version) file. Presently what i am doing is i am reading each line and creating stringtokenizer with comma(,)and reading one by one element.Is this approach is correct or is there any other procedure to read .csv files in java.Thanks in advance
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's how I'd do it.
 
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The trouble with CSV is that there are different flavours. You need to find out the exact spec of the CSV you're trying to import.
For example, some formats allow commas or line-breaks within items. Usually this is done by wrapping the item in single- or double-quotes (e.g. "bloggs, f"). If you simply look at commas you'll end up breaking this item into two pieces when it should be considered as one. Note: there are also different conventions about how to escape quotes within an item.
Once you know which flavour of CSV you're after, search Google. Other people will definitely have solved the problem already. Don't reinvent the wheel.
 
Rehana Shaik
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
my file will contain ,(comma) only .Other than that no special characters are allowed.but my problem is sometimes i will get blank values in between.but StringTokenizer is ignoring blank.Presently we are using jdk1.3 so that i can't use string.split(",").If any one knows the solution please let me know. Thanks in advance
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Solution depends on what kind of file do you want to upload.
In case what u want to upload is an excel, you can use jakarta's POI.
However, in case what you have is an .csv , you can use a utility called SQL Loader.
Enjoy...
 
Author
Posts: 31
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't use StringTokenizer as CSV allows both commans *and* newlines inside quoted fields.

Use a dedicated CSV parser - http://ostermiller.org/utils/CSV.html
[ January 10, 2005: Message edited by: Richard Rodger ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic