File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help Needed regarding reading the csv file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help Needed regarding reading the csv file" Watch "Help Needed regarding reading the csv file" New topic
Author

Help Needed regarding reading the csv file

Keerthi Kumar
Ranch Hand

Joined: Apr 20, 2009
Posts: 105
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 !!


Cheers,
Keerthi Kumar N
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Check out the Excel section of AccessingFileFormats for a few good CSV parsing / writing libraries.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Keerthi Kumar
Ranch Hand

Joined: Apr 20, 2009
Posts: 105
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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: Apr 20, 2009
Posts: 105
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
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

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

Joined: Apr 20, 2009
Posts: 105
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
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
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.


Joanne
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41108
    
  45
Why are you writing parser code yourself? You should use one of the libraries Rob pointed you to.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help Needed regarding reading the csv file
 
Similar Threads
how to load a cvs file into a oracle 9i database using java
read the records from database and write the text file
A Design Question
using BufferedReader and looping
Removing duplicate records