我做的简易文本编译器在保存成txt文档时会出现乱码。如:{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fswiss\fcharset134 \'cb\'ce\'cc\'e5;}{\f1\fnil\fcharset134 \'cb\'ce\'cc\'e5;}}
\viewkind4\uc1\pard\lang2052\f0\fs18\'be\'fc\'b6\'d3\'cb\'be\'c0\'a7\'b7\'a6\'b4\'f3\f1\par
}
在保存成rtb格式时正常。读取txt时正常,读取rtb格式反而出现乱码。
听说是什么UTF-8的问题 。
可我一点也不懂啊
附:核心代码
private void MySaveFile()
{
MyStatus.Text = "保存文件";
if (saveFileDialog1.ShowDialog () == DialogResult.OK )
{
MyRTBox.SaveFile (saveFileDialog1.FileName );
}
}
private void MyOpenFile()
{
CheckSave(); //检查是否保存当前文件
if (openFileDialog1.ShowDialog () == DialogResult.OK )
{
MyRTBox.LoadFile (openFileDialog1.FileName ,RichTextBoxStreamType.PlainText );
MyStatus.Text = "打开文件";
}
}
\viewkind4\uc1\pard\lang2052\f0\fs18\'be\'fc\'b6\'d3\'cb\'be\'c0\'a7\'b7\'a6\'b4\'f3\f1\par
}
在保存成rtb格式时正常。读取txt时正常,读取rtb格式反而出现乱码。
听说是什么UTF-8的问题 。
可我一点也不懂啊
附:核心代码
private void MySaveFile()
{
MyStatus.Text = "保存文件";
if (saveFileDialog1.ShowDialog () == DialogResult.OK )
{
MyRTBox.SaveFile (saveFileDialog1.FileName );
}
}
private void MyOpenFile()
{
CheckSave(); //检查是否保存当前文件
if (openFileDialog1.ShowDialog () == DialogResult.OK )
{
MyRTBox.LoadFile (openFileDialog1.FileName ,RichTextBoxStreamType.PlainText );
MyStatus.Text = "打开文件";
}
}
if(openFileDialog1.ShowDialog ()==DialogResult.OK )
{
FileStream fr=new FileStream(openFileDialog1.FileName ,FileMode.OpenOrCreate ,FileAccess.Read);
byte [] filebtye=new byte[fr.Length ];
fr.Read(filebtye,0,Convert.ToInt32 (fr.Length) );
char [] filechar=new char [fr.Length ];
Encoding myencode=Encoding.Default;
myencode.GetChars (filebtye,0,Convert.ToInt32 (fr.Length) ,filechar,0);
string s= new string (filechar);
fr.Close ();
richTextBox1.Text =s;
}
streamToPrint = new StreamReader(PrintPath);//PrintPath数据流对应的文件的路径
string st1=streamToPrint.CurrentEncoding.EncodingName.ToString();
我对这方面了解不是很深
一般情况下都是Unicode,但是Encoding.Default.GetString(read);
在不同的操作系统中,可能就不相同,Unicode和Default就会有区别,
比如中文系统下的Default用来打印如果处理不好就会使中文编程乱码