File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Reading csv file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Reading csv file " Watch "Reading csv file " New topic
Author

Reading csv file

Rehana Shaik
Ranch Hand

Joined: Jan 03, 2003
Posts: 33
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

Joined: Oct 29, 2001
Posts: 8713
    
    6

That's how I'd do it.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
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

Joined: Jan 03, 2003
Posts: 33
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

Joined: Dec 21, 2003
Posts: 199
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...


~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
Richard Rodger
Greenhorn

Joined: Jan 07, 2005
Posts: 17
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 ]

Richard Rodger<br /><a href="http://www.ricebridge.com" target="_blank" rel="nofollow">http://www.ricebridge.com</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading csv file
 
Similar Threads
reading csv file
Problem in csv file upload
reading a csv file in ant
Creating XML -- Need help
how to write comma in CSV file