This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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
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?
Joined: Apr 20, 2009
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.
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?
Joined: Apr 20, 2009
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.
Joined: Oct 13, 2005
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.
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.
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.