Write/Edit CSV-file (not rewrite the whole file!)

前端 未结 3 1567
挽巷
挽巷 2021-01-27 10:15

I\'m in a situation to replace some functionalities from a client that operates directly on a CSV file, which used as a config file for a system.

Most of the cases avai

3条回答
  •  一向
    一向 (楼主)
    2021-01-27 10:44

    A CSV file is just a sequential text file.

    If you want to modify the contents you have to read the entire file into memory, modify it there and write it out again.

    Assume a more general case of a file that contains the following:

    ABCDEFGHIJKLMNOPQRSTUV

    If you want to remove "IJLKM" from the middle you have to read the remainder of the file (NOP...) so you can shuffle it up to meet (...FGH).

    If you want to insert "0123456789" in between "M" and "N" you need to read N-Z otherwise the new characters will just overwrite "NOPQRSTUVW".

提交回复
热议问题