aspose file tools*
The moose likes I/O and Streams and the fly likes CSV help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "CSV help" Watch "CSV help" New topic
Author

CSV help

jacques du plessis
Greenhorn

Joined: Aug 17, 2007
Posts: 19
Hi,
I have a problem. I have 500 rows and about 12 columns in a csv file. I need to upload the data of specific rows into postgres (but thats not the problem) what I really need to know is how can I isolate the first row which is the header and get a string at a certain position in that first row to upload. Once I have that then I want to skip over to the next line then only is the rest of the data uploaded. I am using BufferedReader and creating an array for each column and thats where I get stuck. I thought I could use a 2 dimensional array like Array[0][1] but it does not work. Please could someone point me in the right direction.
Thank you!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41863
    
  63
Once I have that then I want to skip over to the next line then only is the rest of the data uploaded.

I'mn ot sure what you mean by this. Files are uploaded completely, not partially. I don't think there's an upload API that allows you to look at parts of a file before the upload is complete.

but it does not work.

That's not a useful problem description. What did you do, and what did or did not work?

Is there a particular reason you're writing CSV-handling code yourself instead of using one of the established libraries?


Ping & DNS - my free Android networking tools app
jacques du plessis
Greenhorn

Joined: Aug 17, 2007
Posts: 19
The CSV file is setup like this:
the beginning numbers before the colon is just my representation of the row number and is not part of the csv file.

----------------------------------- |
0:HEADER,DATE,OTHER STUFF |
1:string1,text1,another1,something1 |
2:string2,text2,another2,something2 |
3:string3,text3,another3,something3 |
4:string4,text4,another4,something4 |
5:string6,text5,another5,something5 |
----------------------------------- |
code snippet:
(firstly I open a DB connection and create the BufferedReader object etc)
(this is the code snippet that read the columns of the csv but i also want to read rows so that I can access a string (DATE) in row zero and the skip over once I have it and then populate my DB from row 1)

while((lineReader = in.readLine()) != null){

lineText = lineReader.split(",");
strDate = lineText[0][1].trim(); //I need to get this where
//it corresponds to the
//bold DATE in the csv file
strText = lineText[1].trim(); //this gets treated as a column and
//is inserted into DB table
// and continues till the end of the file
strSomething = lineText[3].trim(); //this gets treated as a column
//and is inserted into DB table
// and continues till the end of the file

(relevant sql goes here to update table)

I hope this make sense

The platform we are working on isn't very forgiving when adding new libraries and it is one of my job requirements to do this.
[ February 22, 2008: Message edited by: jacques du plessis ]
Austin Lee
Ranch Hand

Joined: Feb 18, 2008
Posts: 56
I think you can use the split() to read the csv into a 2 dimensional array.
jacques du plessis
Greenhorn

Joined: Aug 17, 2007
Posts: 19
Tried that but I think if you use split() it only accepts single arrays in other words the columns. I also need to read a row and access a column in a specific row. The idea is not to manipulate the file in any way as it could lead to legal implications due to the nature of the business(we don't want to be blamed for altering a file when we should not have). We just want to read whats there and use the data
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41863
    
  63
It's not quite so easy to handle CSVs. "split" will not be sufficient, as the data items themselves could contain commas. That's why I suggested using an existing library. CSV has more such cases that make parsing it tougher than it at first seems to be. It wouldn't have to be a whole library - you can probably cut it down to a single class that you can pretend you wrote yourself :-)
jacques du plessis
Greenhorn

Joined: Aug 17, 2007
Posts: 19
OK, but this csv has no strings with "," in it, is there any way of doing it?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41863
    
  63
Well, OK. It may work using split, but be aware there are other features of CSV that may trip this up.

I'm not sure what exactly the problem is. You've read the first line and got the one-dimensional array using split, so you should be able to access "DATE" using "lineText[1]". Is that the case? If not, what is the first line being read, and what does split make of it?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

Google for opencsv, that library will take care of all the hard work for you.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
jacques du plessis
Greenhorn

Joined: Aug 17, 2007
Posts: 19
Ok got OpenCSV but how do I incorporate it into my web project?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

Instead of reading the file as you do now, you use the CSVReader:

Optionally, if you use a different separator character (like , instead of ; or vice versa) you can specify that in the CSVReader constructor.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: CSV help