importing the CSV file data in Java

how to import Excel's CSV file data into the Java code,please

Page 1 of 1

4 Replies - 3914 Views - Last Post: 06 November 2012 - 12:33 PM Rate Topic: -----

#1 sabinrjt  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 01-June 09

importing the CSV file data in Java

Posted 02 June 2009 - 01:35 AM

if any one know that please
Is This A Good Question/Topic? 1
  • +

Replies To: importing the CSV file data in Java

#2 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

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

This post has been edited by prajayshetty: 02 June 2009 - 02:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 catseatmice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-November 12

Re: importing the CSV file data in Java

Posted 06 November 2012 - 09:08 AM

View Postsabinrjt, 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
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,213
  • Joined: 20-September 08

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

This post has been edited by g00se: 06 November 2012 - 12:33 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1