建設網(wǎng)站企業(yè)登錄百度怎么提交收錄
目錄
一、BinaryWriter類
二、BinaryReader類
三、示例
1.源碼
2.生成效果
????????二進制文件的寫入與讀取主要是通過BinaryWriter類和BinaryReader類來實現(xiàn)的。
一、BinaryWriter類
????????BinaryWriter類以二進制形式將基元類型寫入流,并支持用特定的編碼寫入字符串,其常用方法及說明:
方 ??法 | 說 ???明 |
Close | 關閉當前的BinaryWriter類和基礎流 |
Seek | 設置當前流中的位置 |
Write | 將值寫入當前流 |
二、BinaryReader類
????????BinaryReader用特定的編碼將基元數(shù)據(jù)類型讀作二進制值,其常用方法及說明:
方??? 法 | 說?? 明 |
Close | 關閉當前閱讀器及基礎流 |
PeekChar | 返回下一個可用的字符,并且不提升字節(jié)或字符的位置 |
Read | 從基礎流中讀取字符,并提升流的當前位置 |
ReadBoolean | 從當前流中讀取Boolean值,并使該流的當前位置提升一個字節(jié) |
ReadByte | 從當前流中讀取下一個字節(jié),并使流的當前位置提升一個字節(jié) |
ReadBytes | 從當前流中將count個字節(jié)讀入字節(jié)數(shù)組,并使當前位置提升count個字節(jié) |
ReadChar | 從當前流中讀取下一個字符,并根據(jù)所使用的Encoding和從流中讀取的特定字符,提升流的當前位置 |
ReadChars | 從當前流中讀取count個字符,以字符數(shù)組的形式返回數(shù)據(jù),并根據(jù)所使用的Encoding和從流中讀取 的特定字符,提升當前位置 |
ReadInt32 | 從當前流中讀取4個字節(jié)有符號整數(shù),并使流的當前位置提升4個字節(jié) |
ReadString | 從當前流中讀取一個字符串。字符串有長度前綴, 一次將7位編碼為整數(shù) |
三、示例
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("要寫入的文件內(nèi)容不能為空");}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); //內(nèi)聯(lián)臨時變量二進制寫入流對象 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) //內(nèi)聯(lián)臨時變量二進制寫入流{ 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 = "文件內(nèi)容:"};// 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.生成效果
?????????操作過程:因為我先前在當前目錄下已經(jīng)存過一個二進制文件了,所以:生成→ 讀取→ 瀏覽到當前目錄,并選擇目錄下的二進制文件,打開顯示在文本框里?!?編輯打開的文件,存儲,可以另存為,也可以覆蓋原文件。→ 再打開文件。
?
?