| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1279 人关注过本帖
标题:[求助]为什么读出来的是乱码
取消只看楼主 加入收藏
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
 问题点数:0 回复次数:9 
[求助]为什么读出来的是乱码

private void button1_Click(object sender, System.EventArgs e)
{
FileStream fs;

if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
textBox2.Text = openFileDialog1.FileName;
try
{
fs = File.OpenRead(textBox2.Text);
byte[] arr = new byte[fs.Length];
UTF8Encoding data =new UTF8Encoding(true);
while(fs.Read(arr,0,arr.Length)>0)
{
textBox1.Text = data.GetString(arr);
}
}
catch
{
MessageBox.Show("读取文件时出错","错误",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);
return;
}

}
}

图片附件: 游客没有浏览图片的权限,请 登录注册

[此贴子已经被作者于2006-9-7 14:29:48编辑过]

搜索更多相关主题的帖子: 乱码 sender byte void 
2006-09-07 14:17
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 
如上图,如果读出文件后,再修改后存入文件,该怎么做?

曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-09-07 14:36
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 
没听懂你说的

曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-09-07 16:31
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 
还是不对啊~~!

曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-09-07 17:00
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 
老大,我用的是UTF8啊~~!

曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-09-07 17:41
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 
不是这个原因~! 郁闷死了,一下午了都没解决~~!

曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-09-07 18:19
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 
中文啊

曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-09-07 18:26
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 
。。。。。

曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-09-07 18:33
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 

完整的代码,帮忙看看
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;

namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button2;

private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();

this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 0;
this.label1.Text = "文件名称";

this.textBox1.Location = new System.Drawing.Point(96, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(176, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";

this.button1.Location = new System.Drawing.Point(304, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 2;
this.button1.Text = "浏览";
this.button1.Click += new System.EventHandler(this.button1_Click);

this.textBox2.Location = new System.Drawing.Point(24, 48);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(432, 184);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";

this.button2.Location = new System.Drawing.Point(392, 8);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(72, 24);
this.button2.TabIndex = 4;
this.button2.Text = "修改";

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(472, 261);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
/*if(open.ShowDialog()==DialogResult.OK)
{
textBox1.Text = open.FileName;
FileStream fs = null;
try
{
fs = File.OpenRead(textBox1.Text);
byte[] content = new byte[fs.Length];
UTF8Encoding data = new UTF8Encoding(true);
while(fs.Read(content,0,content.Length)>0)
{
textBox2.Text += data.GetString(content);
}
}
catch
{
MessageBox.Show("读取文件出错","错误");
return;
}
finally
{
fs.Close();
}
}*/

if(open.ShowDialog()==DialogResult.OK)
{
textBox1.Text = open.FileName;
StreamReader sr = null;
try
{
sr = File.OpenText(textBox1.Text);
String s = null;
while((s=sr.ReadLine())!=null)
{
textBox2.Text += s;
}
}
catch
{
MessageBox.Show("读取文件出错","错误");
return;
}
finally
{
sr.Close();
}
}
}
}
}


曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-09-07 18:36
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 
3Q

曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-09-08 15:09
快速回复:[求助]为什么读出来的是乱码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018356 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved