• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to read .csv file

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How to read .csv file in java?
Thanks,
Angela
 
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Is csv file format same on different OS?
Angela
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it's the same on different OS's.
It's just a text file that uses commas to delimit data.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm, that seems a rather annoying "feature" of the 1.3 StringTokenizer.
Thanks for the tip Jim.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://ostermiller.org/utils/CSVLexer.html
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic