C#生成txt文件


 
  
  1. 代码  
  2.  
  3. using System;  
  4.  
  5. using System.Text;  
  6.  
  7. using System.IO;  
  8.  
  9. namespace Farproc.Text  
  10.  
  11. {  
  12.  
  13.   /// <summary> 
  14.  
  15.   /// 用于取得一个文本文件的编码方式(Encoding)。  
  16.  
  17.   /// </summary> 
  18.  
  19.   public class TxtFileEncoding  
  20.  
  21.   {  
  22.  
  23.     public TxtFileEncoding()  
  24.  
  25.     {  
  26.  
  27.       //  
  28.  
  29.       // TODO: 在此处添加构造函数逻辑  
  30.  
  31.       //  
  32.  
  33.     }  
  34.  
  35.     /// <summary> 
  36.  
  37.     /// 取得一个文本文件的编码方式。如果无法在文件头部找到有效的前导符,Encoding.Default将被返回。  
  38.  
  39.     /// </summary> 
  40.  
  41.     /// <param name="fileName">文件名。</param> 
  42.  
  43.     /// <returns></returns> 
  44.  
  45.     public static Encoding GetEncoding(string fileName)  
  46.  
  47.     {  
  48.  
  49.       return GetEncoding(fileName, Encoding.Default);  
  50.  
  51.     }  
  52.  
  53.     /// <summary> 
  54.  
  55.     /// 取得一个文本文件流的编码方式。  
  56.  
  57.     /// </summary> 
  58.  
  59.     /// <param name="stream">文本文件流。</param> 
  60.  
  61.     /// <returns></returns> 
  62.  
  63.     public static Encoding GetEncoding(FileStream stream)  
  64.  
  65.     {  
  66.  
  67.       return GetEncoding(stream, Encoding.Default);  
  68.  
  69.     }  
  70.  
  71.     /// <summary> 
  72.  
  73.     /// 取得一个文本文件的编码方式。  
  74.  
  75.     /// </summary> 
  76.  
  77.     /// <param name="fileName">文件名。</param> 
  78.  
  79.     /// <param name="defaultEncoding">默认编码方式。当该方法无法从文件的头部取得有效的前导符时,将返回该编码方式。</param> 
  80.  
  81.     /// <returns></returns> 
  82.  
  83.     public static Encoding GetEncoding(string fileName, Encoding defaultEncoding)  
  84.  
  85.     {  
  86.  
  87.       FileStream fs = new FileStream(fileName, FileMode.Open);  
  88.  
  89.       Encoding targetEncoding = GetEncoding(fs, defaultEncoding);  
  90.  
  91.       fs.Close();  
  92.  
  93.       return targetEncoding;  
  94.  
  95.     }  
  96.  
  97.     /// <summary> 
  98.  
  99.     /// 取得一个文本文件流的编码方式。  
  100.  
  101.     /// </summary> 
  102.  
  103.     /// <param name="stream">文本文件流。</param> 
  104.  
  105.     /// <param name="defaultEncoding">默认编码方式。当该方法无法从文件的头部取得有效的前导符时,将返回该编码方式。</param> 
  106.  
  107.     /// <returns></returns> 
  108.  
  109.     public static Encoding GetEncoding(FileStream stream, Encoding defaultEncoding)  
  110.  
  111.     {  
  112.  
  113.       Encoding targetEncoding = defaultEncoding;  
  114.  
  115.       if(stream != null && stream.Length >= 2)  
  116.  
  117.       {  
  118.  
  119.         //保存文件流的前4个字节  
  120.  
  121.         byte byte1 = 0;  
  122.  
  123.         byte byte2 = 0;  
  124.  
  125.         byte byte3 = 0;  
  126.  
  127.         byte byte4 = 0;  
  128.  
  129.         //保存当前Seek位置  
  130.  
  131.         long origPos = stream.Seek(0, SeekOrigin.Begin);  
  132.  
  133.         stream.Seek(0, SeekOrigin.Begin);  
  134.  
  135.         int nByte = stream.ReadByte();  
  136.  
  137.         byte1 = Convert.ToByte(nByte);  
  138.  
  139.         byte2 = Convert.ToByte(stream.ReadByte());  
  140.  
  141.         if(stream.Length >= 3)  
  142.  
  143.         {  
  144.  
  145.           byte3 = Convert.ToByte(stream.ReadByte());  
  146.  
  147.         }  
  148.  
  149.         if(stream.Length >= 4)  
  150.  
  151.         {  
  152.  
  153.           byte4 = Convert.ToByte(stream.ReadByte());  
  154.  
  155.         }  
  156.  
  157.         //根据文件流的前4个字节判断Encoding  
  158.  
  159.         //Unicode {0xFF, 0xFE};  
  160.  
  161.         //BE-Unicode {0xFE, 0xFF};  
  162.  
  163.         //UTF8 = {0xEF, 0xBB, 0xBF};  
  164.  
  165.         if(byte1 == 0xFE && byte2 == 0xFF)//UnicodeBe  
  166.  
  167.         {  
  168.  
  169.           targetEncoding = Encoding.BigEndianUnicode;  
  170.  
  171.         }  
  172.  
  173.         if(byte1 == 0xFF && byte2 == 0xFE && byte3 != 0xFF)//Unicode  
  174.  
  175.         {  
  176.  
  177.           targetEncoding = Encoding.Unicode;  
  178.  
  179.         }  
  180.  
  181.         if(byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF)//UTF8  
  182.  
  183.         {  
  184.  
  185.           targetEncoding = Encoding.UTF8;  
  186.  
  187.         }  
  188.  
  189.         //恢复Seek位置      
  190.  
  191.         stream.Seek(origPos, SeekOrigin.Begin);  
  192.  
  193.       }  
  194.  
  195.       return targetEncoding;  
  196.  
  197.     }  
  198.  
  199.   } 

 

 
  
  1. 代码  
  2.  
  3. using System;  
  4.  
  5. using Farproc.Text;  
  6.  
  7. using System.Text;  
  8.  
  9. using System.IO;  
  10.  
  11. namespace ConsoleApplication1  
  12.  
  13. {  
  14.  
  15.   /// <summary> 
  16.  
  17.   /// Class1 的摘要说明。  
  18.  
  19.   /// </summary> 
  20.  
  21.   class Class1  
  22.  
  23.   {  
  24.  
  25.     /// <summary> 
  26.  
  27.     /// 应用程序的主入口点。  
  28.  
  29.     /// </summary> 
  30.  
  31.     [STAThread]  
  32.  
  33.     static void Main(string[] args)  
  34.  
  35.     {  
  36.  
  37.       //  
  38.  
  39.       // TODO: 在此处添加代码以启动应用程序  
  40.  
  41.       //  
  42.  
  43.       string fileName = @"e:\a.txt";  
  44.  
  45.       //生成一个big endian Unicode编码格式的文本文件  
  46.  
  47.       StreamWriter sw = new StreamWriter(fileName, false, Encoding.BigEndianUnicode);//你可以试试其他编码,比如Encoding.GetEncoding("GB2312")或UTF8  
  48.  
  49.       sw.Write("这是一个String");  
  50.  
  51.       sw.Close();  
  52.  
  53.      //读取  
  54.  
  55.       Encoding fileEncoding = TxtFileEncoding.GetEncoding(fileName, Encoding.GetEncoding("GB2312"));//取得这txt文件的编码  
  56.  
  57.       Console.WriteLine("这个文本文件的编码为:" + fileEncoding.EncodingName);  
  58.  
  59.       StreamReader sr = new StreamReader(fileName, fileEncoding);//用该编码创建StreamReader  
  60.  
  61.       //用下面的方法虽然可以让系统自动判断文本文件的编码格式,但是我们无法取得该文本文件的编码  
  62.  
  63.       //sr.CurrentEncoding永远为 Unicode(UTF-8)  
  64.  
  65.       //StreamReader sr = new StreamReader(fileName, true);  
  66.  
  67.       //Console.WriteLine("这个文本文件的编码为:" + sr.CurrentEncoding.EncodingName);  
  68.  
  69.       Console.WriteLine("这个文本文件的内容为:" + sr.ReadToEnd());  
  70.  
  71.       sr.Close();  
  72.  
  73.       Console.ReadLine();  
  74.  
  75.     }  
  76.  
  77.   }  
  78.  

你先将你导出的东西生成一个txt文件,然后在按钮中加入以下代码
FileName为你导出的txt文件的全路径
FileInfo file = new FileInfo(FileName);//用于获得文件信息
Response.Clear();//清空输出
Response.Charset = "GB2312";//设定编码
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名 
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(FileName));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度 
Response.AddHeader("Content-Length", file.Length.ToString());

// 指定返回的是一个不能被客户端读取的流,必须被下载 
Response.ContentType = "application/ms-txt";

// 把文件流发送到客户端 
Response.WriteFile(file.FullName);



本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1080881

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值