Hero

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Web;
 5using System.Web.Security;
 6using System.Web.UI;
 7using System.Web.UI.WebControls;
 8using System.Web.UI.WebControls.WebParts;
 9using System.Web.UI.HtmlControls;
10
11using System.IO;
12using System.Text;
13
14public partial class _Default : System.Web.UI.Page 
15{
16    protected void Page_Load(object sender, EventArgs e)
17    {
18    
19            string Path = Server.MapPath("7.txt");
20            string str = "我是定时更换 ";
21            insertStr(Path,str,4);
22      
23    }

24
25    public void insertStr(string Path,string str,int n)
26    {
27        string strLineStart = "";
28        string strLineEnd = "";
29        string strLineEnd2 = "";
30        string strLine = "";
31        
32        try
33        {
34            //实例化一个StreamReader对象,并申明编码为GB2312
35            StreamReader sr = new StreamReader(Path, Encoding.GetEncoding("GB2312"));
36           
37            //读取插入前的数据
38            for (int i = 0; i < n; i++)
39            {  
40                strLineStart += sr.ReadLine() + "\r\n";
41
42            }

43
44            //后面的数据
45            while (strLineEnd != null)
46            {
47                strLineEnd = sr.ReadLine();
48                strLineEnd2 += strLineEnd + "\r\n";
49            }

50
51
52            strLine = strLineStart + str + "\r\n" + strLineEnd2;
53
54            //关闭
55            sr.Dispose();
56            sr.Close();
57
58            StreamWriter sw = new StreamWriter(Path, false, Encoding.GetEncoding("GB2312"));
59            sw.WriteLine(strLine);
60
61            //关闭
62            sw.Flush();
63            sw.Dispose();
64            sw.Close();
65              
66        }

67        catch 
68        
69        }

70
71       
72    }

73
74}

75

 

posted on 2008-01-22 15:25  mark620  阅读(685)  评论(0编辑  收藏  举报