在特定的行号上附加文本文件 [英] Append Text File on specific line number

查看:119
本文介绍了在特定的行号上附加文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在检查一个包含将近5000行记录的记事本文件.
我必须在同一文件中每500行之后插入10行记录.
完成这项工作后,我将拥有一个包含总共5100行记录的文件.

Hi to all,

I am checking a notepad file which contains almost 5000 lines of records.
I have to insert 10 rows of records after each 500 lines in the same file.
After doing the job I will have a file which will contain total 5100 lines of records.

Original File
--------------
00001
00002
.
.
00500 <-- New 10 rows after this record
.
.
09999
10000
--------------



我正在使用 System.IO 命名空间用于读取目的.

工具:

框架= 2.0
语言= VB.NET
.
请咨询.
在此先感谢.



I am using System.IO namespace for reading purpose.

Tools:

Framework = 2.0
Language = VB.NET
.
Please Advice.
Thanks In Advance.

推荐答案

文件没有插入"功能:您必须创建具有适当内容的新文件:
从输入文件中逐行读取(您可以使用 StreamReader.ReadLine方法 [^ ]),然后将读取的行输出到ouput文件,直到行数达到500.
然后将"10行"写入输出文件,最后,继续逐行读取输入文件(并将每一行写入输出文件),直到(输入)文件结束.
There''s no ''insertion'' functionality for files: you have to create a new file with the appropriate content:
Read line by line from input file (you may use the StreamReader.ReadLine Method[^]) and output the read line to the ouput file, until line count reaches 500.
Then write the ''10 rows'' to the output file and, finally, resume reading line by line the input file (and write every line to the output file) until the end-of-(input)file.


一种蛮力的方法是将所有行读取到数组或列表中,添加必要的项目,然后将内容写入文件.例如:
One quite brute-force way is to read all the lines to an array or a list, add necessary items and then write the contents to the file. For example something like:
List<string> lines = System.IO.File.ReadAllLines("...").ToList<string>();
lines.Insert(500, "some string");
lines.Insert(501, "other string");
...
System.IO.File.WriteAllLines("...",lines);


这篇关于在特定的行号上附加文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆