开发者

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.

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜