• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading csv file

 
Rehana Shaik
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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
 
Joe Ess
Bartender
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's how I'd do it.
 
David Peterson
author
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Pratibha Malhotra
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • 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...
 
Richard Rodger
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic