深圳做微商网站,注册推广,苏州网站设计价格,关键词 优化 网站目录
一、BinaryWriter类
二、BinaryReader类
三、示例
1.源码
2.生成效果 二进制文件的写入与读取主要是通过BinaryWriter类和BinaryReader类来实现的。
一、BinaryWriter类 BinaryWriter类以二进制形式将基元类型写入流#xff0c;并支持用特定的编码写入字符串#…目录
一、BinaryWriter类
二、BinaryReader类
三、示例
1.源码
2.生成效果 二进制文件的写入与读取主要是通过BinaryWriter类和BinaryReader类来实现的。
一、BinaryWriter类 BinaryWriter类以二进制形式将基元类型写入流并支持用特定的编码写入字符串其常用方法及说明 方 法 说 明 Close 关闭当前的BinaryWriter类和基础流 Seek 设置当前流中的位置 Write 将值写入当前流
二、BinaryReader类 BinaryReader用特定的编码将基元数据类型读作二进制值其常用方法及说明
方 法说 明Close关闭当前阅读器及基础流PeekChar返回下一个可用的字符并且不提升字节或字符的位置Read从基础流中读取字符并提升流的当前位置ReadBoolean从当前流中读取Boolean值并使该流的当前位置提升一个字节ReadByte从当前流中读取下一个字节并使流的当前位置提升一个字节ReadBytes从当前流中将count个字节读入字节数组并使当前位置提升count个字节ReadChar从当前流中读取下一个字符并根据所使用的Encoding和从流中读取的特定字符提升流的当前位置ReadChars从当前流中读取count个字符以字符数组的形式返回数据并根据所使用的Encoding和从流中读取 的特定字符提升当前位置ReadInt32从当前流中读取4个字节有符号整数并使流的当前位置提升4个字节ReadString从当前流中读取一个字符串。字符串有长度前缀 一次将7位编码为整数
三、示例
1.源码
//文件流的二进制读写
//Windows窗体应用.NET8.0,不用设计器
namespace _08
{public partial class Form1 : Form{private Button? button1;private Button? button2;private TextBox? textBox1;private OpenFileDialog? openFileDialog1;private SaveFileDialog? saveFileDialog1;private Label? label1;public Form1(){InitializeComponent();Load Form1_Load;}private void Button1_Click(object? sender, EventArgs e){if (textBox1!.Text string.Empty){MessageBox.Show(要写入的文件内容不能为空);}else{ saveFileDialog1!.Filter 二进制文件(*.dat)|*.dat; //设置保存文件的格式if (saveFileDialog1.ShowDialog() DialogResult.OK){//使用“另存为”对话框中输入的文件名实例化FileStream对象using (FileStream? myStream new(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)){ new BinaryWriter(myStream).Write(textBox1.Text); //内联临时变量二进制写入流对象 new BinaryWriter(myStream).Close(); //关闭当前二进制写入流 myStream.Close(); //关闭当前文件流}textBox1.Text string.Empty;}}}private void Button2_Click(object? sender, EventArgs e){ openFileDialog1!.Filter 二进制文件(*.dat)|*.dat; //设置打开文件的格式if (openFileDialog1.ShowDialog() DialogResult.OK){textBox1!.Text string.Empty;//使用“打开”对话框中选择的文件名实例化FileStream对象using FileStream? myStream new(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);if (new BinaryReader(myStream).PeekChar() ! -1) //内联临时变量二进制写入流{ textBox1.Text Convert.ToString(new BinaryReader(myStream).ReadString()); //以二进制方式读取文件} new BinaryReader(myStream).Close(); //关闭当前二进制读取流 myStream.Close(); //关闭当前文件流}}private void Form1_Load(object? sender, EventArgs e){// textBox1textBox1 new TextBox{Location new Point(12, 29),Multiline true,Name textBox1,Size new Size(270, 111)};// label1label1 new Label{AutoSize true,Location new Point(12, 9),Text 文件内容};// button1button1 new Button{Location new Point(66, 146),Name button1,Size new Size(75, 23),Text 写入,UseVisualStyleBackColor true};button1.Click Button1_Click;// button2button2 new Button{Location new Point(147, 146),Name button2,Size new Size(75, 23),Text 读取,UseVisualStyleBackColor true};button2.Click Button2_Click;// openFileDialog1openFileDialog1 new OpenFileDialog{FileName openFileDialog1};//saveFileDialog1saveFileDialog1 new SaveFileDialog();//Form1AutoScaleDimensions new SizeF(7F, 17F);AutoScaleMode AutoScaleMode.Font;ClientSize new Size(295, 180);StartPosition FormStartPosition.CenterScreen;Controls.Add(label1);Controls.Add(textBox1);Controls.Add(button2);Controls.Add(button1);} }
}2.生成效果 操作过程因为我先前在当前目录下已经存过一个二进制文件了所以生成→ 读取→ 浏览到当前目录并选择目录下的二进制文件打开显示在文本框里。→ 编辑打开的文件存储可以另存为也可以覆盖原文件。→ 再打开文件。