IO filestream 将流输入到文件中

 

代码如下;

View Code
  string path = @"c:\temp\wtq_1.txt";
            
byte[] byteDate=new Byte[1000];
            
char[] c;
            
byte[] d;
            
try
            {
                
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
                {
                    
string str = "我叫***,为什么我使用这种方式不会乱码";
                    c 
= str.ToCharArray();
                    Console.WriteLine(c.Length);
                    Console.ReadLine();
                    
foreach (var item in c)
                    {
                        Console.Write(item);   
                    }
                   
//byteDate = new byte[c.Length*2];
                   var Enc = Encoding.UTF8.GetEncoder();
                   Enc.GetBytes(c, 
0, c.Length, byteDate, 0true);
                   fs.Seek(
0, SeekOrigin.Begin);
                   fs.Write(byteDate,
0,byteDate.Length);
             
                  
                }
            }
            
catch (Exception e)
            {

            }

 

这样可以把汉字写入到txt中,
posted on 2011-08-01 21:55  wtq  阅读(291)  评论(0编辑  收藏  举报