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


    //可以操作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();

  • 相关阅读:
    [Leetcode] Flatten Binary Tree to Linked List
    [Leetcode] Letter Combinations of a Phone Number
    [Leetcode] Distinct Subsequences
    [Leetcode] Partition List
    [Leetcode] Reverse Linked List II
    [Leetcode] Swap Nodes in Pairs
    [Jobdu] 题目1391:顺时针打印矩阵
    [Jobdu] 题目1369:字符串的排列
    [Jobdu] 题目1283:第一个只出现一次的字符
    如何应用ML的建议-下
  • 原文地址:https://www.cnblogs.com/sdya/p/4963915.html
Copyright © 2020-2023  润新知