IO filestream 将流输入到文件中

本文介绍了一种使用UTF-8编码将包含汉字的字符串写入到txt文件中的方法,包括字符转换、编码获取及文件操作等步骤。

 

代码如下;

ExpandedBlockStart.gifView 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中,

转载于:https://www.cnblogs.com/85538649/archive/2011/08/01/2124306.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值