110,536
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
load();
update();
}
public void load()
{
//向文件中写入数据,并清空以前文件中的数据。
StreamWriter sw = new StreamWriter(Server.MapPath(".") + "\\file.txt");
//向文件中写入数据,不清空以前文件中的数据。
//StreamWriter sw = File.AppendText(Server.MapPath(".") + "\\file.txt");
for (int i = 0; i < 10; i++)
{
sw.WriteLine(i.ToString());
}
////sw.Write("接着写");
sw.Flush();
sw.Close();
}
public void update()
{
//读取
StreamReader sr = new StreamReader(Server.MapPath(".") + "\\file.txt");
ArrayList arrlist = new ArrayList();
string str = "";
Response.Write("修改前<BR>");
do
{
str = sr.ReadLine();
if (str != null)
{
arrlist.Add(str);
Response.Write(str + "<BR>");
}
} while (str != null);
sr.Dispose();
sr.Close();
//修改
Response.Write("修改后<BR>");
for (int i = 0; i < arrlist.Count; i++)
{
if (arrlist[i].ToString()=="5")
{
arrlist[i] = "55";
}
Response.Write(arrlist[i].ToString() + "<BR>");
}
StreamWriter sw = new StreamWriter(Server.MapPath(".") + "\\file.txt");
for (int i = 0; i < arrlist.Count; i++)
{
sw.WriteLine(arrlist[i].ToString());
}
////sw.Write("接着写");
sw.Flush();
sw.Close();
//读取结果表明
StreamReader streamReader = new StreamReader(Server.MapPath(".") + "\\file.txt");
string ss = "";
do
{
ss = streamReader.ReadLine();
if (str != null)
{
Response.Write(ss + "<BR>");
}
} while (ss != null);
streamReader.Dispose();
streamReader.Close();
}
}
//读取文本
StreamReader sr = new StreamReader(文本文件的路径);
string str = sr.ReadToEnd();
sr.Close();
//替换文本
string [] result = str.Split("\n".ToCharArray());//数组中一个元素代表一行
result[index]="***";//想改哪行自己决定
//更改保存文本
StreamWriter sw = new StreamWriter(文本文件的路径,false);
foreach(string s in result)
sw.WriteLine(s);
sw.Close();