if any one know that please
importing the CSV file data in Javahow to import Excel's CSV file data into the Java code,please
Page 1 of 1
4 Replies - 1852 Views - Last Post: 06 November 2012 - 12:33 PM
Replies To: importing the CSV file data in Java
#2
Re: importing the CSV file data in Java
Posted 02 June 2009 - 01:40 AM
it is not so tough but it is not so easy to you will require 3 things first how to use string tokenizer and second a some no how of file streams and some info on java
no one is going to write code for you
no one is going to write code for you
This post has been edited by prajayshetty: 02 June 2009 - 02:46 AM
#3
Re: importing the CSV file data in Java
Posted 06 November 2012 - 09:08 AM
sabinrjt, on 02 June 2009 - 01:35 AM, said:
if any one know that please
this guy did write it: http://beginwithjava...ile-reader.html ,
and here's his code, which works perfectly well and was hugely helpful to me, especially to learn how to do it: I hope others find it helpful too!
// CSVRead.java
//Reads a Comma Separated Value file and prints its contents.
import java.io.*;
import java.util.Arrays;
public class CSVRead{
public static void main(String[] arg) throws Exception {
BufferedReader CSVFile =
new BufferedReader(new FileReader("Example.csv"));
String dataRow = CSVFile.readLine(); // Read first line.
// The while checks to see if the data is null. If
// it is, we've hit the end of the file. If not,
// process the data.
while (dataRow != null){
String[] dataArray = dataRow.split(",");
for (String item:dataArray) {
System.out.print(item + "\t");
}
System.out.println(); // Print the data line.
dataRow = CSVFile.readLine(); // Read next line of data.
}
// Close the file once all data has been read.
CSVFile.close();
// End the printout with a blank line.
System.out.println();
} //main()
} // CSVRead
#4
Re: importing the CSV file data in Java
Posted 06 November 2012 - 10:15 AM
StringTokenizer is legacy code
Use a Scanner to read the file line per line using scanner.nextLine()
use the String class split(",") method to split that line into an array of token
Use a Scanner to read the file line per line using scanner.nextLine()
use the String class split(",") method to split that line into an array of token
#5
Re: importing the CSV file data in Java
Posted 06 November 2012 - 12:33 PM
A word of warning: the following is a perfectly legimate 3-field csv line, which will break your code if you use String.split:
a,"foo,bar",b
Better to use a proper csv parser such as Ostermiller
a,"foo,bar",b
Better to use a proper csv parser such as Ostermiller
This post has been edited by g00se: 06 November 2012 - 12:33 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|