简体   繁体   English

如何替换文本文件中已经存在的文本,以及如何在现有文本的行之间插入文本

[英]How to replace the text that was already exists in a text file and how can i insert text in between the lines of the existing text

Hi all i have a text file saved with some data as follows 大家好,我有一个文本文件,其中保存了一些数据,如下所示

101011111111101111111111009100954A094101                                                      
9000000000001000000000000000000000000000001000000000000000000000000000000000000000000000000000

Now i would like to insert data in between these 2 lines as 现在我想在这两行之间插入数据

52201               1                   1         CCD1         100910100910   1111111110000001
6211111111181                00000000011              1                     1 0111111110000001
822000000101111111180000000000000000000000011                                  111111110000001

and the final output should be as follows 最终输出应如下

101011111111101111111111009100954A094101                                                      
52201               1                   1         CCD1         100910100910   1111111110000001
6211111111181                00000000011              1                     1 0111111110000001
822000000101111111180000000000000000000000011                                  111111110000001
9000000000001000000000000000000000000000001000000000000000000000000000000000000000000000000000

and also i would like to replace some values in the last line basing on the lines inserted in between first and lines. 我也想根据第一行和第二行之间插入的行替换最后一行中的一些值。 So can any one give me an idea 谁能给我一个主意

You have to read the file into a data structure (a list of lines, for example). 您必须将文件读入数据结构(例如,行列表)。

Then process the data as you would in memory, creating a new list of lines. 然后像处理内存一样处理数据,创建新的行列表。

Then write them to file. 然后将它们写入文件。

For large files use streams instead. 对于大文件,请使用流。

Some operating systems have special file formats where you can treat lines in a file almost like a database: add, update and delete lines as you wish. 某些操作系统具有特殊的文件格式,您可以在其中像对待数据库一样对待文件中的行:根据需要添加,更新和删除行。 I don't believe that you can do this in Windows. 我不相信您可以在Windows中执行此操作。

So you would need to read the original file and write the modified file to temporary file then delete (or save) the original file and rename the new file. 因此,您需要读取原始文件并将修改后的文件写入临时文件,然后删除(或保存)原始文件并重命名新文件。

If the whole file is small read it into memory, identify each line and keep them in a data structure such as a List. 如果整个文件很小,则将其读入内存,标识每一行并将其保留在数据结构(如列表)中。

If the file is bigger than will fit in memory then you need to read it and rewrite it in chunks until you find the plkace where you are changing and make the modfications before you rewrite. 如果文件大于内存大小,则需要读取它并分块重写,直到找到要更改的地方并在重写之前进行修改。

You didn't specify what you've done already, and which part of your project you need help with. 您没有指定已经完成的工作以及需要帮助的项目的哪一部分。

Assuming that you have a string with data and want to insert something between the lines, this code would do that: 假设您有一个包含数据的字符串,并且想在行之间插入一些内容,则此代码将执行以下操作:

// Two lines of data
string data = 
    "101011111111101111111111009100954A094101\n" + 
    "9000000000001000000000000000000000000000001000000000000000000000000000000000000000000000000000";

// New data
string newdata =
    "52201 1 1 CCD1 100910100910 1111111110000001 6211111111181 00000000011 1 1 0111111110000001 822000000101111111180000000000000000000000011 111111110000001";

// Insert the new data after the first line change
data = data.Insert(data.IndexOf("\n") + 1, newdata + "\n");

Console.WriteLine(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM