使用I/O流操作文本文件


string p = Server.MapPath("") + "//1.txt";  //操作文件的路径
using (StreamWriter sw = new StreamWriter(p, true))   //写入
{
    for (int i = 0; i < 10; i++)
    {
        sw.WriteLine(i);
    }
    sw.Close();
}

 

using (StreamReader sr = new StreamReader(p))   //读取
{
    string data = sr.ReadToEnd();
    sr.Close();
    Response.Write(data);
}

使用I/O流操作二进制文件

string p = Server.MapPath("") + "//1.txt";

//写入   

       FileStream fs = new FileStream(p, FileMode.OpenOrCreate, FileAccess.ReadWrite);


           BinaryWriter bw = new BinaryWriter(fs);// fs实例化BinaryWriter二进制写入流对象
           bw.Write("ss");

           bw.Close();
           fs.Close();

 

//读取

           FileStream fs2 = new FileStream(p, FileMode.Open, FileAccess.Read);
           BinaryReader br = new BinaryReader(fs2);
           if (br.PeekChar() != -1)
           {
               string data = br.ReadString();
               Response.Write(data);
           }
           br.Close();
           fs2.Close();

posted @ 2012-01-31 16:38  高捍得  阅读(195)  评论(0编辑  收藏  举报