读取txt后显示出乱码!如何解决

Ly_uu 2006-06-20 01:57:50
我在页面上面读取一txt文档! 然后显示在页面中! 为什么会乱码呢?

cs:
public string js;
using (StreamReader sr = new StreamReader(@"D:\1.txt"))
{
string line;

while((line = sr.ReadLine())!=null)
{
js += line;
}
}

页面:

<%= js%>
...全文
922 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
nyzfl 2007-03-06
  • 打赏
  • 举报
回复
lz 楼上已经有正解
lgnet 2007-03-06
  • 打赏
  • 举报
回复
顶呀
hartnio 2007-03-06
  • 打赏
  • 举报
回复
这是一个编码问题
.txt中的编码是UTF-8
在网页中默认是ISO-8859-1
这样就出现了乱码的显示问题啦
csdnhbgjh 2007-03-05
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class Test_txt : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//String filenameStr = Server.MapPath("jgdm.txt");
//StreamReader objStreamReader=new StreamReader();
//String objStreamReader = File.OpenText(FILENAME);
//String contents = objStreamReader.ReadToEnd();
//Response.Write(contents);


StreamReader sr = new StreamReader("F:\\jgdm.txt", System.Text.Encoding.GetEncoding("gb2312"));

String line;

while ((line = sr.ReadLine()) != null)
{

Label1.Text = line.ToString();

}
sr.Close();

}

}


读出结果:
"002523403","鹿城区科学技术协会","7","O9400","1 ","330302550604081 ","002523008","黄晓兵","330300","330300","325000","2004-11-29 10:35:48.11","","","蛟凤北路108号","","0577-88627160","2004-11-29 10:34:00.4","","","","2","2002-06-15 00:00:00.0","2008-11-29 00:00:00.0","0.0","","","","2005-11-29 00:00:00.0","0","2004-11-29 10:34:00.4","0","1","鹿城区委办公室","1","综合管理全取科普工作","1","","","","","","鹿委办[2002]45号","","","","","虞冰翰3303","虞冰翰330302660227322","","","","","","支行瑞信分理处","3207309064012829-","2004-11-29 10:35:48.11",""
zdqluck 2006-06-23
  • 打赏
  • 举报
回复
写的时间定义
System.IO.StreamWriter sw=new System.IO.StreamWriter(FStream,System.Text.Encoding.GetEncoding("gb2312"));
就可以了
活靶子哥哥 2006-06-23
  • 打赏
  • 举报
回复
StreamReader 有一个带有 Enconding 参数的 构造函数
insiderc 2006-06-23
  • 打赏
  • 举报
回复
using (StreamReader sr = new StreamReader(myFile.Name,System.Text.Encoding.GetEncoding("GB2312"),true))
insiderc 2006-06-23
  • 打赏
  • 举报
回复
//参考
private void btn_import_Click(object sender, System.EventArgs e)
{
FileInfo myFile=new FileInfo(this.ofd.FileName);
try
{
if(!myFile.Exists)
{
MessageBox.Show(this.ofd.FileName+"不存在!","文件不存在",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}

StringCollection sc=new StringCollection();
using (StreamReader sr = new StreamReader(myFile.Name,System.Text.Encoding.GetEncoding("GB2312"),true))
{
string oneLine="";
do
{
if ((oneLine=sr.ReadLine())!=null)
sc.Add(oneLine);
}
while(oneLine!=null);
}
string [] sa=new string[sc.Count];
sc.CopyTo(sa,0);
this.txt_SqlScript.Lines=sa;
}
catch(Exception err)
{
MessageBox.Show(err.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return ;
}

}
cancerser 2006-06-23
  • 打赏
  • 举报
回复
txt 文件编码格式 是什么?
pclion 2006-06-23
  • 打赏
  • 举报
回复
加了楼上的这段,还是乱码
llainn 2006-06-20
  • 打赏
  • 举报
回复
<!-- 全球化
此节设置应用程序的全球化设置。
-->
<globalization
requestEncoding="gb2312"
responseEncoding="gb2312"
uiCulture="zh-CN"
culture="zh-CN"
fileEncoding="GB2312"
/>
x_cong 2006-06-20
  • 打赏
  • 举报
回复
<globalization
requestEncoding="GB2312"
responseEncoding="GB2312"
culture="zh-CN" fileEncoding="GB2312"
/>
在Web.Config文件的这块这样设置一下试试....

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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