C#如何将byte[]写入文件的方法和注意的问题

来源:互联网 发布:淘宝商品标题怎么优化 编辑:程序博客网 时间:2024/06/05 05:06


解决:

File.WriteAllBytes
http://msdn.microsoft.com/zh-cn/library/system.io.file.writeallbytes.aspx


其他:

这里要注意,byte[]数组里面可能有不可见字符,所以程序里不要进行如GetString()之类的转换,这样会出错的,对一些不可见的字符会有乱码。可以用写二进制流的方式进行读写文件即可。

FileStream fs1 = new FileStream(@"E:\tenp\doc\111.txt", FileMode.Open, FileAccess.Read, FileShare.Read);FileStream fs2 = new FileStream(@"E:\temp\doc\222.txt", FileMode.Create, FileAccess.Write, FileShare.None);byte []farr = new byte[1024];const int rbuffer=1024;//fs1.ReadByte(); //读取单个字节,返回-1表示读完while (fs1.Read(farr, 0, rbuffer)!=0) //返回0表示读完{fs2.Write(farr, 0, rbuffer);}fs1.Close();fs2.Close();



原创粉丝点击