用c#读取文件二进制流的问题

xunaipeng 2009-03-11 11:38:22
在c#中用FileStream读取文件,用reader.ReadChar(),一个字节一个字节的读,但是文件中用汉字,请问高手怎么判断读到的下一个字节是汉字!!
谢谢!!!
...全文
1135 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
justindreams 2009-03-16
  • 打赏
  • 举报
回复
难道不能一行一行的读吗?非要一个字节一个字节的读取?

using (FileStream fs = new FileStream("文件路径", FileMode.Open))
{
using (StreamReader reader = new StreamReader(fs, Encoding.Default))
{
string line = reader.ReadLine();//读取文件中每一行字符串
while(line != null)
{
//操作
}
}
}
xunaipeng 2009-03-16
  • 打赏
  • 举报
回复
虽然没有达到我想象中的效果,也解决了,结贴!
谢谢各位
gisyellow 2009-03-15
  • 打赏
  • 举报
回复
一个汉字占两个字节,英文字符只占一个字节。所以你可以两个两个字节的读取,如果第一个字节为0,则是英文,否则是中文字符。
ppppph 2009-03-14
  • 打赏
  • 举报
回复
用正则表达式阿
AdaEniac 2009-03-13
  • 打赏
  • 举报
回复
为什么非得一个字节一个字节的读?
xunaipeng 2009-03-13
  • 打赏
  • 举报
回复
我问题是要一个字节一个字节的读!!
xunaipeng 2009-03-13
  • 打赏
  • 举报
回复
我的文件内容是这样的
Class 新类名1
Class liu
Class 马
Class 新类名3
Class Layer-mj
Class Layer-121
Class Layer-a
Class Layer-fy
Class 扶余-mj
Class Layer-jing
Class Layer-m
Class 黑帝庙-MJ
Class LAYER
Class 葡316-葡462扶二油层-mj
Class 葡316-葡462扶一含油-mj
Class 葡南地区葡316-葡462扶余油层含油面积
Class Layer-x
Class 葡萄花油田南部扶余油层含油面积叠合图
Class 青一-mj
Class 萨尔图-mj
Class 他拉哈fy-mj
Class 他拉哈f
Class 他拉哈填充

这个文件的后缀名是.dfd,我现在想把这些内容读过来,然后再写到TXT中
我是这样读的
using(fs = new FileStream(filePath,FileMode.Open,FileAccess.Read))
{
reader = new BinaryReader(fs);
reader.BaseStream.Seek(0,SeekOrigin.Begin);

ArrayList[] al=new ArrayList[20];
for(int i=0;i<20;i++)
{
al[i]=new ArrayList();
}


int j = 0;
string temp = "";
for(int i=0;i<reader.BaseStream.Length;i++)
{
string name1=reader.ReadChar().ToString();

// temp = temp +name1;
// if(name == "\r")
// {
// string name1 = reader.ReadChar().ToString();
// if(name1 == "\n")
// {
// temp=temp+"\n";
// al[j].Add(temp);
// j++;
// temp = "";
// }
// if(j%20==0 && j!=0)
// {
// j=0;
// }
// string name2 = reader.PeekChar().ToString();
// if(name2 == "13")
// {
// string name3 = reader.ReadChar().ToString ();
// string name4 = reader.ReadChar().ToString ();
// if(name4 == "\n")
// {
//
// }
// }
// }

}
this.CreateDataToTXT(al);
}

当读到汉字的时候,如读到“Class 新类名1”后,在txt文本中显示的是“Class 1”,它就好像没读汉字一样,不知道为什么,大侠们帮帮忙啊!
飞天鹰 2009-03-12
  • 打赏
  • 举报
回复
同意楼上的说法
whowhen21 2009-03-12
  • 打赏
  • 举报
回复
可以根据字符的ASSCII 判断呀,这样应该可以的哦。
withcsharp 2009-03-12
  • 打赏
  • 举报
回复
 
http://ctm1688.blog.163.com/blog/static/60837602200871210235065/

 //读取

      Encoding fileEncoding = TxtFileEncoding.GetEncoding(fileName, Encoding.GetEncoding("GB2312"));//取得这txt文件的编码

      Console.WriteLine("这个文本文件的编码为:" + fileEncoding.EncodingName);

      StreamReader sr = new StreamReader(fileName, fileEncoding);//用该编码创建StreamReader

      //用下面的方法虽然可以让系统自动判断文本文件的编码格式,但是我们无法取得该文本文件的编码

      //sr.CurrentEncoding永远为 Unicode(UTF-8)

      //StreamReader sr = new StreamReader(fileName, true);

      //Console.WriteLine("这个文本文件的编码为:" + sr.CurrentEncoding.EncodingName);

      Console.WriteLine("这个文本文件的内容为:" + sr.ReadToEnd());

      sr.Close();

      Console.ReadLine();

xunaipeng 2009-03-12
  • 打赏
  • 举报
回复
问题是我不知道一次读多少
zgke 2009-03-12
  • 打赏
  • 举报
回复
这个没办法..你还是一起读出来把..
yueding_520 2009-03-12
  • 打赏
  • 举报
回复
使用序列化啊!

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧