怎样替换TXT文件中的内容?

sorsirzeng 2004-11-25 10:21:49
Dear All,
我可以用StreamReader和StreamWriter读取和写入TXT文件。请问怎样把TXT文件中的某行替换成其它内容? 谢谢

Kent
...全文
447 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hl80 2004-11-25
  • 打赏
  • 举报
回复
用reader循环时,定义个i让其++,当到你想要的行数时把那行取出来替换掉不行吗?
yichuan1982 2004-11-25
  • 打赏
  • 举报
回复
你必须在那一行有特殊标记
你读出来以后,把那行有标记的替换就可以了:
刚才才帖了一下代码,现在再帖一遍,参考生成表态HTML的代码,对你的问题有帮助:

public static bool WriteFile(string strText,string strContent,string strAuthor)
{
string path = HttpContext.Current.Server.MapPath("/news/");
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("/news/text.html");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}


string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str =str.Replace("ShowArticle",strText); //模板页中的ShowArticle
str = str.Replace("biaoti",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// 写文件
try
{
sw = new StreamWriter(path + htmlfilename , false, code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;

此函数放在Conn.CS基类中了
在添加新闻的代码中引用 注:工程名为Hover

if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出错!");
}
-------------------------------------------------------------------------
模板页Text.html代码
-------------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ShowArticle</title>

<body>

biaoti
<br>
content<br>
author
</body>
</HTML>






chyich 2004-11-25
  • 打赏
  • 举报
回复
读出它的内容到一个string,然后用string.replace方法来替换内容,再把替换好的字符串重新保存.
46539492 2004-11-25
  • 打赏
  • 举报
回复
跟替换html文件一样的做法
----------------------
html模板页面(template.htm)
-------------
<html>
<head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  </head>
  <body>
  <table bgcolor=$htmlformat[0] height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor="#eeeeee" style="border:1px solid #000000">
  <tr>
  <td width="100%" valign="middle" align="left">
  <span style="color: $htmlformat[1];font-size: $htmlformat[2]"> $htmlformat[3] </span>
  </td>
  </tr>
  </table>
  </body>
  </html>

----------------------
在asp.net里面替换$htmlformat
<%@ Page Language="vb" Debug="True" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.io" %>
<Script language="vb" Runat="Server">
sub Page_Load(Sender as object, E as EventArgs)
dim format(3) as String
dim htmltext as String
dim file2 as system.io.file
dim sr as StreamReader = file2.opentext(server.mappath("template.htm"))
dim line as string
line=sr.ReadLine()
do while not line=nothing
htmltext=htmltext & line
line=sr.ReadLine()
loop
sr.Close()
format(0)="#ffccff"
format(1)= "#990099"
format(2)="9px"
format(3)= "<font size=9pt>生成的模板html页面</font>"
dim i as integer
for i=0 to 3
htmltext=htmltext.Replace("$htmlformat[" & i & "]",format(i))
next
dim file1 as system.io.file
dim sw as StreamWriter=new StreamWriter(server.mappath("3.htm"),false,System.Text.Encoding.GetEncoding("GB2312"))
sw.WriteLine(htmltext)
sw.Flush()
sw.Close()

end sub
</Script>

jxzhang615 2004-11-25
  • 打赏
  • 举报
回复
帮顶!

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧