为什么我读文件的时候没有读出汉字?

bethrezen 2003-05-20 09:46:09
文件STREETNAME.txt如下:
Data

none
Text
"程"
498142.9943 18069.6168 498183.8081 18103.6284
Font ("Arial",0,0,32896)

Text
"序"
498999.7146 17996.0782 499040.5284 18030.0897
Font ("Arial",0,0,32896)

Text
"员"
498575.0322 18031.9282 498615.846 18065.9398
Font ("Arial",0,0,32896)
我的代码如下:
StreamReader sr = File.OpenText("F:\\STREETNAME.txt");
string str;
int j;
while((str=sr.ReadLine())!=null)
{
arrItem.Add(str.Trim());
}
for(j=0;j<arrItem.Count;j++)
{
if(arrItem[j].ToString()=="Text")
{
break;
}
}
for(int i=j;i<arrItem.Count-1;i++)
{
listBox1.Items.Add(arrItem[i]);
}

为什么程序员3个字都显示的是""?怎么样才能显示出来?
...全文
34 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
colin666 2003-05-20
  • 打赏
  • 举报
回复
up
xingzhiyun 2003-05-20
  • 打赏
  • 举报
回复
⑥(读Text)StreamReader
int a=0;
StreamReader sr;
switch(a)//3种打开方式
{
case 0:
// sr=new StreamReader(@"d:\temp\b.txt",System.Text.Encoding.GetEncoding("GB2312") ,true);
sr=new StreamReader(@"d:\temp\b.txt",System.Text.Encoding.Default,true);
break;
case 1:
FileStream fsr=new FileStream(@"d:\temp\b.txt",FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
sr=new StreamReader(fsr,System.Text.Encoding .Default);
break;
default:
FileInfo MyFile=new FileInfo(@"d:\temp\b.txt");
sr=MyFile.OpenText();
//用windows的记事本写的中文文件,这里无法读出(乱码),但用streamwrite写的文件可以读出
break;
}

Console.WriteLine(sr.CurrentEncoding );
string s;
int nextchar;
int i=0;
//3种读出方式
if(i==0)//逐行读出
for(;true;)
{
s=sr.ReadLine();//逐行读出
if(s!=null) Console.WriteLine(s);
else break;
//sr.ReadToEnd();//从当前位置开始将流的全部内容读完.
}
else if(i==1)//逐字读出,汉字也可以读出
{
while(sr.Peek()!=-1)//Peek并不会使指针向前移动,只是读一下而已
{
nextchar=sr.Read();//汉字也可以读出
Console.WriteLine((char)nextchar);
}
}
else if(i==2)//字串读出
{
int l;
l=100;
char[] chararr=new char[l];
sr.Read(chararr,0,l);//虽然汉字占2个字节,但读取汉字的时候汉字只占一个位置
for(int j=0;j<l;j++)
Console.WriteLine(chararr[j]);
}
sr.Close();
czsd 2003-05-20
  • 打赏
  • 举报
回复
up
banping 2003-05-20
  • 打赏
  • 举报
回复
up
bethrezen 2003-05-20
  • 打赏
  • 举报
回复
An unhandled exception of type 'System.PlatformNotSupportedException' occurred in mscorlib.dll

Additional information: PlatformNotSupportedException

请问在PocketPC中该怎么办?是PocketPC不支持中文吗?
chinchy 2003-05-20
  • 打赏
  • 举报
回复
StreamReader sr = File.OpenText("F:\\STREETNAME.txt");==>
StreamReader sr=new StreamReader("F:\\STREETNAME.txt",System.Text.Encoding.GetEncoding("GB2312"));
freeboy0002 2003-05-20
  • 打赏
  • 举报
回复

FileStream fs = new FileStream(strFileName, FileMode.Open,FileAccess.Read);
StreamReader file = new StreamReader(fs,Encoding.GetEncoding("gb2312"));

110,539

社区成员

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

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

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