文件的读写操作

protected void Button1_Click(object sender, EventArgs e)
    {
        //创建文件
        //方法一:利用FileStream类和StreamWriter类创建
        //string path = @"c:\test.html";
        //FileStream fs = new FileStream(path, FileMode.Create);
        //StreamWriter sw = new StreamWriter(fs,Encoding.GetEncoding("GB2312"));
        //sw.Write("Hi,你好!<br/>");
        //sw.Write("javaeye,你好!");
        //sw.Close();//关闭流
        //sw.Dispose();//释放资源

        //方法二:利用File类创建并使用AppendAllText方法
        //string path1 = @"c:\test2.txt";
        //File.AppendAllText(path1, "欢迎来到陶院!");
        //sw.Close();
        //sw.Dispose();

        //方法三:使用数组传入,使用File类
        //string path1 = @"c:\test3.txt";
        //string []content={"明天上课","晚上见面!"};
        //File.WriteAllLines(path1, content);
        //sw.Close();
        //sw.Dispose();

        //方法四:利用利用FileInfo类和StreamWriter类创建
        //string path2 = @"c:\test3.doc";
        //FileInfo fl = new FileInfo(path2);
        //using (StreamWriter sw = fl.CreateText())
        //{
        //    sw.WriteLine("今年是2011年");
        //    sw.WriteLine("元旦快乐!");
        //}
        //sw.Close();
        //sw.Dispose();



        //读取文件
        //方法一:利用StreamReader类和ReadLine方法读取
        //string path2 = @"c:\test2.doc";
        //StreamReader sr = new StreamReader(path2);
        //string con= sr.ReadLine();
        //string content = sr.ReadToEnd();
        //Response.Write(con+"<br/>");
        //Response.Write(content);
        //方法二:利用File类和ReadAllLines方法传给数组变量conn
        //string path2 = @"c:\test2.doc";
       //string []conn=File.ReadAllLines(path2, Encoding.UTF8);
       //foreach (string  str in conn)
       //{
       //    Response.Write(str+"<br/>");
       //}
        //方法三:利用FileStream类读取
        //string path2 = @"c:\test2.doc";
        //FileStream fs = new FileStream(path2, FileMode.Open);
        //byte[] array = new byte[30];
        //fs.Read(array, 0, 30);
        //string con = Encoding.UTF8.GetString(array);
        //Response.Write(con);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值