• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Help Needed regarding reading the csv file

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have to write one java application, which will read the csv file and write the columns into a hashmap. For every 20 records, I need to read the csv file, add ito a hashmap and write it to a new csv file. Csv file will be having more than 1000 records. So for every 20 records, i need to write the contents to a new csv file. Request you to please help me out in this.

Thanks in advance !!
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you got so far and where are you stuck?

Looking at the requirement, I would split it into small requirements.
1) Code to access the file contents
1.a) Code to read the contents line by line
2) Maintain a counter for the number of lines read
3) Process read data on the fly (after reading every line) or in a batch (after reading 20 lines)
3.a) Extract the read data into individual column values
4) Code to write the read data to a new csv file

Hint: CSV format usually indicates that the data is going to be textual, with commas as delimiters
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the Excel section of AccessingFileFormats for a few good CSV parsing / writing libraries.
 
Keerthi Kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

Thanks a lot for your reply. As per your below reply, I have completed

1) Code to access the file contents
1.a) Code to read the contents line by line
2) Maintain a counter for the number of lines read

Now I need to write the contents that I am reading from csv to a hashmap and then from map to a new csv for every 20 set of records. Please see my code below for your reference.

 
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using StringTokenizer? Why not use a Scanner with its delimiter set to a comma? That would probably be easier. Or read line by line and use String#split?

What are you doing with the Map? Have you simply allowed an IDE to fill in the code for you, because I don't think the line with put in will compile?
 
Keerthi Kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,

The code in my previous post will surely throw a compile time exception. I forgot to mention that. But the thing is i need to add those csv contents into map. Then from map i need to write the contents to a new csv file. Could you please help me out in this wit my below code.

Thanks in advance !
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you want your map to be structured?
I noticed <Integer,String>. So thats line number,line? or tokenNumber,token?
Anyway you need to populate your map according to your requirement.

Since you have code to read from a file, it should be simple to write back. Conceptually it is exactly the opposite of reading from the file.
Which part are you stuck in writing?
 
Keerthi Kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maneesh,

Thanks a lot for your reply. Say my csv file is having 1000 records. Instead of reading/writing at one shot, I need to read 20 records at one shot, write it to a map and from map write the contents to a new csv file. Im stuck up in the place where i need to check for every 20th record !

Please do the needful.
 
Campbell Ritchie
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you writing the file to a Map? If you simply want it in line number order, a List<String> will do just as well.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Keerthi Kumar wrote:Hi Rob,

Thanks a lot for your reply.


Why are you thanking Rob when you seem to have ignored his reply ?
In general, what Campbell says in his first post is good advice, but when it comes to CSV files this quote from Rob's link is worth reading

CSV is not as easy to read and write as it first looks - once all the special cases are considered, one might as well use a library.

 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You already have lineNumber and tokenNumber. You are not using it.
For every line, check of the line/token number has reached the desired count. If yes, proceed to the file writing, if no, continue reading.

Agree with Campbell. List would be a better option.
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you writing parser code yourself? You should use one of the libraries Rob pointed you to.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic