Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CSV help

 
jacques du plessis
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
jacques du plessis
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can use the split() to read the csv into a 2 dimensional array.
 
jacques du plessis
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, but this csv has no strings with "," in it, is there any way of doing it?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Google for opencsv, that library will take care of all the hard work for you.
 
jacques du plessis
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok got OpenCSV but how do I incorporate it into my web project?
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic