首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

字符串数组转化成字节数组提示出错,该如何解决

2012-01-08 
字符串数组转化成字节数组提示出错代码如下我用的是2003引用的类:usingSystemusingSystem.DrawingusingS

字符串数组转化成字节数组提示出错
代码如下   我用的是2003
引用的类:
using   System;
using   System.Drawing;
using   System.Collections;
using   System.ComponentModel;
using   System.Windows.Forms;
using   System.Data;
using   System.IO;
using   System.Text;

调用的代码:
private   void   button2_Click(object   sender,   System.EventArgs   e)
{
byte[]   bydata;
char[]   chardata;
FileStream   aFile=new   FileStream( "temp.txt ",FileMode.Create);
chardata= "My   pink   half   of   the   drainpipe. ".ToCharArray();
bydata=new   byte[chardata.Length];
Encoder   e=Encoding.UTF8.GetEncoder();
e.GetBytes(chardata,0,chardata.Length,bydata,0,true);
aFile.Seek(0,SeekOrigin.Begin);
aFile.Write(bydata,0,bydata.Length);
}

其中
Encoder   e=Encoding.UTF8.GetEncoder();
e.GetBytes(chardata,0,chardata.Length,bydata,0,true);
提示出错,这句注视以后,新建了文件,但是没有写入字符流

[解决办法]
aFile.Write(chardata,0,chardata.Length);
不就行了 干嘛要中间那么复杂
编码可以用StreamWriter指定 FileStream不是很好用
[解决办法]
操作文件用StreamWriter写字符,没有必要操作字节,效率慢。
StreamWriter实现了TextWriter,使其以一种特定的编码向流中写入字符。

热点排行