aspose file tools*
The moose likes Java in General and the fly likes How to write into specific fields in CSV file using IO? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to write into specific fields in CSV file using IO?" Watch "How to write into specific fields in CSV file using IO?" New topic
Author

How to write into specific fields in CSV file using IO?

Akhilesh Murthy
Greenhorn

Joined: Oct 07, 2009
Posts: 24

Hi, I want to write into a CSV file, into specific fields of the file.
Can i write the specific fields in file using IO?

Thanks,
Akhilesh
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Check for CSV in AccessingFileFormats page.
Akhilesh Murthy
Greenhorn

Joined: Oct 07, 2009
Posts: 24

Hi The file formats had more of parsers and reader API's. My requirement was mainly for writing into specific fields of the file.

Thanks
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Akhilesh Murthy wrote:Hi The file formats had more of parsers and reader API's. My requirement was mainly for writing into specific fields of the file.

Thanks


You can't write directly into a single field of a CSV file. You have to read the file into memory, change what you want to change, and write it back out. Note that you can operate on one line at a time--you don't necessarily have to have the entire file in memory.
Akhilesh Murthy
Greenhorn

Joined: Oct 07, 2009
Posts: 24

As a .csv file is stored in form of an excel the fields are populated based on the date written. Now my question is how can i write into a specific cell of the file. i.e, specific cell of the excel file.

Thanks Akhilesh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Moving thread as too difficult a question for “beginning”
Don’t know, but investigate something called POI from the Apache foundation.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Akhilesh Murthy wrote:As a .csv file is stored in form of an excel


No, a CSV file is a "comma-separated-value" file. That is, a plain text file that uses commas (or some other delimiter) do separate one field from another.

the fields are populated based on the date written. Now my question is how can i write into a specific cell of the file. i.e, specific cell of the excel file.


As I said: Read the file, find the appropriate place, change what you need to change, write the object model back out to the file.

This is the only way to do it; you can't just modify some part of the file directly, unless you a) know ahead of time the exact byte position of where you want to make the change AND b) the change you make will not use any more bytes than were already allocated for that field in that file. (And if it meets those conditions, then it's not really a CSV file.)

Now, there may be libraries out there--such as POI or a CSV manipulation library--that will abstract away the details of finding the cell and re-writing the entire file, so that you can just do something like setValue(row, column, value). If that's what you're looking for, see the link in the first reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to write into specific fields in CSV file using IO?