文件处理,替换文本文件中的特定行 [英] File Handling, replacing a certain line in a text file

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

问题描述

使用循环搜索文件直到到达特定行,如何用我定义的我自己的行替换(删除并写入)文件中的该特定行?

在我的示例中,我知道需要覆盖的特定行始终是第一行.我该怎么做?

Using a loop to search through a file until a certain line is reached, how do I replace (remove and then write) that specific line in the file with my own line that I define?

In my example I know that the specific line that needs to be overwritten is always the first. How do I do this?

推荐答案

使用此代码,这可能对您有帮助

use this code , this may helps you

//Read all lines from the file
string[] data=File.ReadAllLines("myfile.txt");
//replace the first line with your data.
 data[0] = "my own text";
//write all data again into the file.
 File.WriteAllLines("myfileout.txt", data);



谢谢
--RA



Thanks
--RA


string[] lines = File.ReadAllLines("file.txt");

lines[0]="New Line to overwrite";

File.WriteAllLines("file.txt", lines );


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

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