当前位置:编程学习 > C#/ASP.NET >>

学生发帖求指教用BinaryReader和BinaryWriter读写二进制文件问题

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace BinaryIO
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string fileName = "MyNew.data";//定义字符串变量存储文件名字符串
            if (File.Exists(fileName))//判断文件是否存在
            {
                MessageBox.Show("当前文件已经存在");
            }
            else
            {
                FileStream fs = new FileStream(fileName, FileMode.Create);
                //使用FileStream类创建文件
                BinaryWriter writer = new BinaryWriter(fs);//将BinaryWriter类实例化
                writer.Write(textBox1.Text);
                //调用BinaryWriter类的Write()方法将文本框中的数据写入
                //for (int i = 0; i < 300; i++)
                //{
                //    writer.Write(10 + i);
                //}
                    MessageBox.Show("写入文件成功");
                textBox1.Text = "";//关闭BinaryWriter流
                fs.Close();//关闭FileStream流
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string fileName = "MyNew.data";//定义字符串变量存储文件名字符串
            if (!(File.Exists(fileName)))//判断要读取的文件是否存在
            {
                MessageBox.Show("当前文件不存在");
                return;
            }
            string strData = "";
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            //以读取已有文件的方式创建FileStream的实例对象
            BinaryReader reader = new BinaryReader(fs);//实例化BinaryReader类
            strData = reader.ReadString();//调用BinaryReader的ReadString方法
            for (int i = 0; i < 200; i++)//循环读取文件内容
            {
                if (i == 0)
                {
                    strData += reader.ReadInt32().ToString();
                    //每次读取4个字节带符号的整数值,并转换为字符串类型
                }
                else
                {
                    strData += " || " + reader.ReadInt32().ToString();
                }
            }
            textBox2.Text = strData;//读取的文件内容显示在textBox2中
            fs.Close();//关闭文件流对象
            reader.Close();//关闭二进制文件读对象存储文件名字符串
            
        }
    }
}

这个是我的窗体界面
在写数据到指定文件时,用
writer.Write(textBox1.Text);
点击“读取文件”,会出现
“在 System.IO.EndOfStreamException 中第一次偶然出现的“mscorlib.dll”类型的异常
未处理的“System.IO.EndOfStreamException”类型的异常出现在 mscorlib.dll 中。

其他信息: 无法在流的结尾之外进行读取。”
//for (int i = 0; i < 300; i++)
                //{
                //    writer.Write(10 + i);
                //}
写数据,点“读取文件”,就啥都不显示,并且打开“MyNew。data”文件里面都是乱码。这是为啥,求指教 --------------------编程问答-------------------- 在读取方法处,设置一个断点,然后单步调试执行,看会不会发生异常。
可以看看官方的例子。http://msdn.microsoft.com/zh-cn/library/system.io.binaryreader.aspx --------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string fileName = "MyNew.data";//定义字符串变量存储文件名字符串
            if (File.Exists(fileName))//判断文件是否存在
            {
                MessageBox.Show("当前文件已经存在");
            }
            else
            {
                FileStream fs = new FileStream(fileName, FileMode.Create);
                //使用FileStream类创建文件
                BinaryWriter writer = new BinaryWriter(fs);//将BinaryWriter类实例化
                writer.Write(textBox1.Text);
                //调用BinaryWriter类的Write()方法将文本框中的数据写入
                for (int i = 0; i < 300; i++)
                {
                    writer.Write(10 + i);
                }
                MessageBox.Show("写入文件成功");
                textBox1.Text = "";//关闭BinaryWriter流
                fs.Close();//关闭FileStream流
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string fileName = "MyNew.data";//定义字符串变量存储文件名字符串
            if (!(File.Exists(fileName)))//判断要读取的文件是否存在
            {
                MessageBox.Show("当前文件不存在");
                return;
            }
            string strData = "";
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            //以读取已有文件的方式创建FileStream的实例对象
            BinaryReader reader = new BinaryReader(fs);//实例化BinaryReader类
            strData = reader.ReadString();//调用BinaryReader的ReadString方法
            for (int i = 0; i < 300; i++)//循环读取文件内容
            {
                if (i == 0)
                {
                    strData += reader.ReadInt32().ToString();
                    //每次读取4个字节带符号的整数值,并转换为字符串类型
                }
                else
                {
                    strData += " || " + reader.ReadInt32().ToString();
                }
            }
            textBox2.Text = strData;//读取的文件内容显示在textBox2中
            fs.Close();//关闭文件流对象
            reader.Close();//关闭二进制文件读对象存储文件名字符串        
        }
    }
}


测试成功。 --------------------编程问答--------------------
引用 2 楼 caozhy 的回复:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string fileName = "MyNew.data";//定义字符串变量存储文件名字符串
            if (File.Exists(fileName))//判断文件是否存在
            {
                MessageBox.Show("当前文件已经存在");
            }
            else
            {
                FileStream fs = new FileStream(fileName, FileMode.Create);
                //使用FileStream类创建文件
                BinaryWriter writer = new BinaryWriter(fs);//将BinaryWriter类实例化
                writer.Write(textBox1.Text);
                //调用BinaryWriter类的Write()方法将文本框中的数据写入
                for (int i = 0; i < 300; i++)
                {
                    writer.Write(10 + i);
                }
                MessageBox.Show("写入文件成功");
                textBox1.Text = "";//关闭BinaryWriter流
                fs.Close();//关闭FileStream流
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string fileName = "MyNew.data";//定义字符串变量存储文件名字符串
            if (!(File.Exists(fileName)))//判断要读取的文件是否存在
            {
                MessageBox.Show("当前文件不存在");
                return;
            }
            string strData = "";
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            //以读取已有文件的方式创建FileStream的实例对象
            BinaryReader reader = new BinaryReader(fs);//实例化BinaryReader类
            strData = reader.ReadString();//调用BinaryReader的ReadString方法
            for (int i = 0; i < 300; i++)//循环读取文件内容
            {
                if (i == 0)
                {
                    strData += reader.ReadInt32().ToString();
                    //每次读取4个字节带符号的整数值,并转换为字符串类型
                }
                else
                {
                    strData += " || " + reader.ReadInt32().ToString();
                }
            }
            textBox2.Text = strData;//读取的文件内容显示在textBox2中
            fs.Close();//关闭文件流对象
            reader.Close();//关闭二进制文件读对象存储文件名字符串        
        }
    }
}


测试成功。
“using System.Threading.Tasks;”vs2008用不了这个。。 --------------------编程问答--------------------
引用 3 楼 hhhgreen 的回复:
Quote: 引用 2 楼 caozhy 的回复:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string fileName = "MyNew.data";//定义字符串变量存储文件名字符串
            if (File.Exists(fileName))//判断文件是否存在
            {
                MessageBox.Show("当前文件已经存在");
            }
            else
            {
                FileStream fs = new FileStream(fileName, FileMode.Create);
                //使用FileStream类创建文件
                BinaryWriter writer = new BinaryWriter(fs);//将BinaryWriter类实例化
                writer.Write(textBox1.Text);
                //调用BinaryWriter类的Write()方法将文本框中的数据写入
                for (int i = 0; i < 300; i++)
                {
                    writer.Write(10 + i);
                }
                MessageBox.Show("写入文件成功");
                textBox1.Text = "";//关闭BinaryWriter流
                fs.Close();//关闭FileStream流
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string fileName = "MyNew.data";//定义字符串变量存储文件名字符串
            if (!(File.Exists(fileName)))//判断要读取的文件是否存在
            {
                MessageBox.Show("当前文件不存在");
                return;
            }
            string strData = "";
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            //以读取已有文件的方式创建FileStream的实例对象
            BinaryReader reader = new BinaryReader(fs);//实例化BinaryReader类
            strData = reader.ReadString();//调用BinaryReader的ReadString方法
            for (int i = 0; i < 300; i++)//循环读取文件内容
            {
                if (i == 0)
                {
                    strData += reader.ReadInt32().ToString();
                    //每次读取4个字节带符号的整数值,并转换为字符串类型
                }
                else
                {
                    strData += " || " + reader.ReadInt32().ToString();
                }
            }
            textBox2.Text = strData;//读取的文件内容显示在textBox2中
            fs.Close();//关闭文件流对象
            reader.Close();//关闭二进制文件读对象存储文件名字符串        
        }
    }
}


测试成功。
“using System.Threading.Tasks;”vs2008用不了这个。。


我只是贴我完整的代码啦。没用到的。 --------------------编程问答-------------------- 你报异常出在strData += reader.ReadInt32().ToString();而不是ReadString。
因为你文件中没有足够的整数。 --------------------编程问答--------------------
引用 4 楼 caozhy 的回复:
Quote: 引用 3 楼 hhhgreen 的回复:

Quote: 引用 2 楼 caozhy 的回复:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string fileName = "MyNew.data";//定义字符串变量存储文件名字符串
            if (File.Exists(fileName))//判断文件是否存在
            {
                MessageBox.Show("当前文件已经存在");
            }
            else
            {
                FileStream fs = new FileStream(fileName, FileMode.Create);
                //使用FileStream类创建文件
                BinaryWriter writer = new BinaryWriter(fs);//将BinaryWriter类实例化
                writer.Write(textBox1.Text);
                //调用BinaryWriter类的Write()方法将文本框中的数据写入
                for (int i = 0; i < 300; i++)
                {
                    writer.Write(10 + i);
                }
                MessageBox.Show("写入文件成功");
                textBox1.Text = "";//关闭BinaryWriter流
                fs.Close();//关闭FileStream流
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string fileName = "MyNew.data";//定义字符串变量存储文件名字符串
            if (!(File.Exists(fileName)))//判断要读取的文件是否存在
            {
                MessageBox.Show("当前文件不存在");
                return;
            }
            string strData = "";
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            //以读取已有文件的方式创建FileStream的实例对象
            BinaryReader reader = new BinaryReader(fs);//实例化BinaryReader类
            strData = reader.ReadString();//调用BinaryReader的ReadString方法
            for (int i = 0; i < 300; i++)//循环读取文件内容
            {
                if (i == 0)
                {
                    strData += reader.ReadInt32().ToString();
                    //每次读取4个字节带符号的整数值,并转换为字符串类型
                }
                else
                {
                    strData += " || " + reader.ReadInt32().ToString();
                }
            }
            textBox2.Text = strData;//读取的文件内容显示在textBox2中
            fs.Close();//关闭文件流对象
            reader.Close();//关闭二进制文件读对象存储文件名字符串        
        }
    }
}


测试成功。
“using System.Threading.Tasks;”vs2008用不了这个。。


我只是贴我完整的代码啦。没用到的。
writer.Write(textBox1.Text);
                //调用BinaryWriter类的Write()方法将文本框中的数据写入
                for (int i = 0; i < 300; i++)
                {
                    writer.Write(10 + i);
                }
这两个语句不是将数据写入文件的两种方法吗,为什么注释掉一种就不能成功运行呢?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,