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 the .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 the .csv file" Watch "how to read the .csv file" New topic
Author

how to read the .csv file

Eung maeng
Ranch Hand

Joined: Feb 10, 2002
Posts: 68
Dear guys,
Could you please help me if there is anyone has same experience to read from .csv File?
I'm stuck here to read. My customer has oracle system will provide the .csv file to retrieve the information.
I wonder whether java has a solution to read .csv file easily. If not, what's the best way to create the table from .csv file.
I really appreciated for your reply.
look forward to hear from you soon,
sincerely,
paul maeng
pmaeg@kaita.org.au
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
Hi!
Java by itself does not offer read-made solutions to read a CSV file. Use a buffered Reader to read the file line by line. And for each line use the StringTokenizer class to get the individual column values. The code below would serve u as a hint
BufferedReader brFile = new BufferedReader(new FileReader(fileName));
StringTokenizer rowTokens;
String row;
row = brFile.readLine();
while(row!=null)
{
rowTokens = new StringTokenizer(row, ",");
// do whatever u want
row = brFile.readLine();
}
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"pmaeng",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp.
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please edit your profile and select a new name which meets the requirements.
Thanks.
Dave
Eung maeng
Ranch Hand

Joined: Feb 10, 2002
Posts: 68
Originally posted by Sudharsan G'rajan:
[qb]Hi!
thanks a lot for this.
I'm actuallytrying to get whole each line through the tokenizer. However, I'm still stuck here to read the each line.
1 BufferedReader brFile = new BufferedReader(new
2 FileReader(file));
3 StringTokenizer rowTokens;
4 String row;
5 row = brFile.readLine();
6 while(row!=null)
7 {
8 rowTokens = new StringTokenizer(row, ",");
9 // do whatever u want

10 row = brFile.readLine();
11 System.out.println("row"+row);
12 }
Can I see the row value in line 11 after read or not? I got the strange chacters like broken
chacter.
e.g "D9iF86vner arowucoeDddDoadrv;P=0row}cNne7s"2.....
Thus, I would like to see your example source for treading .csv file correctly. It would be really appreciated.
Hope, look forward to hearing from you soon,
paul maeng
[ September 24, 2002: Message edited by: pmaeng ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"pmaeng",
Please change your display name, since accounts with illegal display names get deleted.
If your name is Paul Maeng, please edit your profile and change your display name.
Thanks,
Dave.
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
Hi!
Place the System.out.println() before the brFile.readLIne() method and try it.

Also check whether the CSV file is in ASCII text
Richard Rodger
Greenhorn

Joined: Jan 07, 2005
Posts: 17
StringTokenizer will not work as CSV files allow quoted fields to contain commas and newlines.

The best solution is to use a proper CSV library:
http://www.google.ie/search?q=java+csv+library


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: how to read the .csv file