文件流的具体操作应用和三种汇出的方式

//可以操作txt/xml等
//在d盘创建一个abc.txt文件,并且往里面写数据
FileStream fs = new FileStream("d:\\abc.txt", FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.Write("123"); sw.Flush(); sw.Close(); fs.Close();
//可以操作txt/xml等
//读取d盘的a.txt内文件的内容

FileStream fs = new FileStream("d:\\a.txt", FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            string sLine = "";
            string abc = "";
            ArrayList LineList = new ArrayList();
            while (sLine != null)
            {
                sLine = sr.ReadLine();
                if (sLine != null && !sLine.Equals(""))
                {
                   abc =abc+ sLine+"   ";
                }
            }
            return abc;

导出的方法总结

方法一:
HttpResponse resp = System.Web.HttpContext.Current.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(FileName));
resp.Write(sb.ToString());

方法二:
Console.WriteLine(sb.ToString());
FileStream aFile = new FileStream("D:\\temp\\lintest.dat", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile);
sw.Write(sb.ToString());
sw.Close();

方法三: 下载到客户端
Response.Clear();
Response.Buffer = true;
Response.ContentType = "text/richtext";
Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8) + ";size=0");
Response.Write(sb.ToString());
Response.Flush();
Response.End();

 

posted @ 2015-11-14 09:42  好好學習  阅读(337)  评论(0编辑  收藏  举报