Forums Register Login

how to read the .csv file

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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();
}
+Pie Number of slices to send: Send
"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
+Pie Number of slices to send: Send
 

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 ]

 
+Pie Number of slices to send: Send
"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.
+Pie Number of slices to send: Send
Hi!
Place the System.out.println() before the brFile.readLIne() method and try it.

Also check whether the CSV file is in ASCII text
+Pie Number of slices to send: Send
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
Crusading Chameleon likes the size of this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1921 times.
Similar Threads
CSV to Java
Problem in reading csv file
How an XML document is written into CSV format in Java/JSP
JDBC Driver
i need to read files with dateformat filename from a folder
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:51:01.