Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to read the .csv file

 
Eung maeng
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic