日期:2014-05-19  浏览次数:20860 次

字符串数组转化成字节数组提示出错
代码如下   我用的是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,使其以一种特定的编码向流中写入字符。