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 How to read .csv file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to read .csv file " Watch "How to read .csv file " New topic
Author

How to read .csv file

Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
Hi,
How to read .csv file in java?
Thanks,
Angela
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Ok, well .csv files have data within a row delimited by commas.
So given this you can make use of the StringTokenizer class to easily return an enumeration of the data elements per row.
Here is an outline of the sort of thing you would want to do:
Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
Thanks, Is csv file format same on different OS?
Angela
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Yes, it's the same on different OS's.
It's just a text file that uses commas to delimit data.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Note that StringTokenizer won't work the way you want if some of the values are allowed to be blank. E.g. in the input
Allan,Burt,Carl
Amelia,,Charlotte
Ace,Butch,Cujo
the second field of the second line is probably supposed to be blank. However a StringTokenizer will treat Charlotte as the value for the second field, and there will be no third field for that line. Which could be a problem. The easiest solution is probably to use SDK 1.4, which adds a split() method to String:


"I'm not back." - Bill Harding, Twister
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Hmmm, that seems a rather annoying "feature" of the 1.3 StringTokenizer.
Thanks for the tip Jim.
Steve Deadsea
Ranch Hand

Joined: Dec 03, 2001
Posts: 125
http://ostermiller.org/utils/CSVLexer.html
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Hi Steve,
I've downloaded your utility. My problem is that the separator in the file that i receive is a semicolon ( ; ) instead of a coma (,). I went around the methods to see if it was possible to set a different separator, but couldn't find anything. Do you have any suggestion, or will I have to parse all the file and replace the semicolon by a coma. The file I'm receiving should be an Excell file.
Thx in adance for any answers
[ January 03, 2003: Message edited by: Younes Essouabni ]

Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
In one objectmentor.com article a CSV parser is created using Test Driven Development (don't look at the one labeled 'circa 1998'--it's not nearly as easy to work with). If you understand Finite State Machines the code there should be easy to modify, and if you don't than look at the method charEvent(char) and change it to understand semicolons.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to read .csv file
 
Similar Threads
How an XML document is written into CSV format in Java/JSP
Problem in reading csv file
How to read csv file & which collection should be used ?
CSV to Java
display csv file as an excel in java