0

I would like to update a .csv file containing 1 million records. I want to update in the same file without creating another file. I am using Opencsv. I don't find any way to update the .csv file. If anyone can help me out that would be great. For clarification let's say these small .csv file:

Initial csv file:

a,b,c,d
1,2,3,4
5,6,7,8
12,13,14,15

Desired csv file:

a,b,c,d,e,f
1,2,3,4, ,17
5,6,7,8,16,
12,13,14,15,
user3387358
  • 45
  • 1
  • 6
  • Update means what you are going to do. Please explain – Siva Kumar Dec 11 '14 at 09:38
  • I have to add new header filed and data for the field. Let's say currently I have a file with 4 headers a,b,c,d and 4 rows of data. I want to add a new header 'e' with some data. – user3387358 Dec 11 '14 at 09:42
  • Normally file system doesnt allow to update the file. You need to rewrite the file. Because even though you have all in one file . it will be written in multiple segments. So if you are going to change the file segments will be damaged. So you need to rewrite every thing. – Siva Kumar Dec 11 '14 at 09:44
  • Why dont you create a new file and replace with existing at the end? – janasainik Dec 11 '14 at 09:44

2 Answers2

0

Basically - you cannot do that.

As csv does not use fixed-length records, most changes will require moving data up or down in the file. That cannot be done without completely rewriting the file.

1,2,3,4
5,6,7,8
...

changing the 8 to a 10 would require every byte from that location onwards in the whole file to be moved up one position.

To achieve the effect of editing the file it is usual to copy the file to a new name making the change you wish to make during the copy. You would then rename the two files so that the new one replaces the old one.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • Thanks for the reply. I wanted to find some way and avoid rewriting the file as it contains 1 million records. Reading 1 million records and writing it back would take some time in the program that I want to avoid. – user3387358 Dec 11 '14 at 09:44
  • @user3387358 - If you choose a fixed-length record format you can edit it in-place using a [RandomAccessFile](https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html). Alternatively - use a database. – OldCurmudgeon Dec 11 '14 at 09:46
  • I have to add a new header with some data I don't have to change existing headers.I have a file with 4 headers a,b,c,d and 4 rows of data. I want to add a new header 'e' with some data. – user3387358 Dec 11 '14 at 09:47
  • @user3387358 - Not sure I understand - you said you had 1 million records - now you have 4??? You could consider adding new records on the end of the file if that solves your problem. – OldCurmudgeon Dec 11 '14 at 09:49
  • This is just to tell you what I want I can't write here 1 million records. Please see my question for clarification. I have updated it. – user3387358 Dec 11 '14 at 09:55
  • @user3387358 - Understood - no you cannot do that in-place, you must create a new file. This would not be so difficult if you used a database. – OldCurmudgeon Dec 11 '14 at 10:32
0

Normally file system doesnt allow to update the file. You need to rewrite the file. Because even though you have all in one file . it will be written in multiple segments. So if you are going to change the file segments will be damaged. So you need to rewrite every thing.

Siva Kumar
  • 1,983
  • 3
  • 14
  • 26