c# BinaryReader 无法再流的结尾之外进行读取
以下是代码usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSyst...
以下是代码
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 BinaryRW二进制文件{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类的Writer方法将文本框中的数据写入 for (int i = 0; i < 300; i++){writer.Write(10 + i);}MessageBox.Show("写入文件成功");textBox1.Text = "";writer.Close(); //关闭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(); //关闭二进制文件读对象存储文件名字符串}}} 展开
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 BinaryRW二进制文件{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类的Writer方法将文本框中的数据写入 for (int i = 0; i < 300; i++){writer.Write(10 + i);}MessageBox.Show("写入文件成功");textBox1.Text = "";writer.Close(); //关闭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(); //关闭二进制文件读对象存储文件名字符串}}} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询