PDA

Click to See Complete Forum and Search --> : [RESOLVED] Java CSVFileReader


xxarmoxx
Jan 31st, 2008, 12:47 PM
How do I import the java CSVFileReader and how do I use it?

Greyskull
Jan 31st, 2008, 03:09 PM
Well what kind of things you want to do with the CSV file?

You simply need to add the relevant header file. Depending on the method chosen.

http://www.exampledepot.com/egs/java.util.regex/ParseLine.html - this mite be of help

If you simply want to open the actual CSV file then 'import java.io.*;' should do the trick with the following code:


Process p = Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler " +
"strFileLocation");


where strFileLocation = the location of the file you want to open

Regards,

xxarmoxx
Jan 31st, 2008, 03:16 PM
the problem is when Im tokenizing the line with commas, im missing cells with commas in them. The way csv files are created is if there is a comma in the cell then quotes are put around the token. I read that there is a built in way of taking care of this common probelm in php and im sure something exists with java, i just would like to know what it is and how to use it.

Greyskull
Jan 31st, 2008, 03:18 PM
Here you could also try this out:


String [][] strNum = new String [30][30];

File f = new File("filename.csv");

BufferedReader br = new BufferedReader(new FileReader(f));
String strLine = null;
int row = 0;
int col = 0;

//read each line of text file
while((strLine = br.readLine()) != null && row < 30)
{
StringTokenizer st = new StringTokenizer(strLine,",");
while (st.hasMoreTokens())
{
//get next token and store it in the array
strNum[row][col] = st.nextToken();
col++;
}
col = 0;
row++;
}

Greyskull
Jan 31st, 2008, 03:19 PM
not sure whether the above post will help you though (didnt know you had commas in your CSV file)...what kind of data is stored in your CSV?

ComputerJy
Jan 31st, 2008, 05:02 PM
not sure whether the above post will help you though (didnt know you had commas in your CSV file)...what kind of data is stored in your CSV?
CSV = Comma Separated Values.
This means any kind of values

Greyskull
Jan 31st, 2008, 05:10 PM
CSV = Comma Separated Values.
This means any kind of values

Sorry i think i said that wrong :blush: . i knw what CSV file is but what i was trying to say was im not sure how to do it if one of his values contained a comma as i knw that the application will think its the end of line...

ComputerJy
Jan 31st, 2008, 05:23 PM
when using CSV, all commas in original data must be encoded to something like a tag so it can be decoded when reading the file without causing any problems

xxarmoxx
Jan 31st, 2008, 06:04 PM
So do you guys know where I can get a class that works?

Greyskull
Feb 1st, 2008, 05:41 AM
The following link maybe of help

http://faq.javaranch.com/java/AccessingFileFormats

Under the Excel files, there are three links you could try with CSV files.

tbh, i havent really tried inserting commas to any of the fields stored in a CSV file as i knew i will encounter problems such as this, instead, if i had to store i value containing commas, i will use a database.

Regards,

xxarmoxx
Feb 1st, 2008, 12:09 PM
Cool, that looks pretty solid. I used this one, it works great

http://opencsv.sourceforge.net/

Greyskull
Feb 1st, 2008, 02:03 PM
Glad to hear it :thumb: Don't forget to resolve the thread ;)