二进制文件的读写
private void button3_Click(object sender, EventArgs e)
{
BinaryWriter bw;
FileStream fs = new FileStream( "c:\\mydata.data ", FileMode.Create);
bw = new BinaryWriter(fs);
for (int i = 0; i < 200; i++)
bw.Write( "dgdfgdfgdfg ");//写入
bw.Close();//关闭
}
private void button4_Click(object sender, EventArgs e)
{
BinaryReader br;
string str = " ";
FileStream fs = new FileStream( "c:\\mydata.data ", FileMode.Open);
br = new BinaryReader(fs);
for (int i = 0; i < fs.Length / 4; i++)
str += br.ReadInt32().ToString();
textBox2.Text = str;
}
读出来全是:168449741917178561021734763623168449741917178561021734763623168449741917178561021734763623168449741917178561021734763623168449741917178561021734763623.......
我想问一下,怎么样将读出来的数据转换为写进去的数据呢
------解决方案--------------------这就是ascii码,你按字符读取
------解决方案--------------------摘录:《程序员秘书》--源代码--目录及文件--读写二进制文件
立即成为编程经验丰富的程序员不是梦,详见:http://www.psec.net.cn
6、在Form1.cs的视图设计器中,选中button1,在属性框中选中事件,双击Click,在Form1.cs的代码设计器中,添加修改如下代码
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
{
FileStream FS = new FileStream(SaveFileDialog1.FileName, FileMode.Create);//文件不存在则新建
BinaryWriter BWriter = new BinaryWriter(FS);//以二进制打开文件流
byte[] buffer = Encoding.Default.GetBytes(richTextBox1.Text);//可以用不同的字符编码
BWriter.Write(bu