C# 中无法在流的结尾之外进行读取处理BinaryReader()
来源:互联网 发布:lua用什么软件 编辑:程序博客网 时间:2024/05/21 17:58
再窗口体中放置1各编辑框控件,用来输入小于255得数,放置2个按钮控件,标题分别“输入下一个”和“存入输入得数”。单击标题为“输入下一个数按钮”记录输入得数,清空编辑控件,准被输入下一个数 。单击标题存“入输入得数”的按钮,存入文件中。再加一个按钮“读文件”的按钮,读入文件内容,在listbox控件显示。
//自动代码
namespace l_6_1
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.butnext = new System.Windows.Forms.Button();
this.butsave = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(137, 6);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 0;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// butnext
//
this.butnext.Location = new System.Drawing.Point(14, 33);
this.butnext.Name = "butnext";
this.butnext.Size = new System.Drawing.Size(76, 21);
this.butnext.TabIndex = 1;
this.butnext.Text = "输入下一个";
this.butnext.UseVisualStyleBackColor = true;
this.butnext.Click += new System.EventHandler(this.button1_Click);
//
// butsave
//
this.butsave.Location = new System.Drawing.Point(111, 32);
this.butsave.Name = "butsave";
this.butsave.Size = new System.Drawing.Size(76, 23);
this.butsave.TabIndex = 2;
this.butsave.Text = "存输入得数";
this.butsave.UseVisualStyleBackColor = true;
this.butsave.Click += new System.EventHandler(this.butsave_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(193, 33);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 3;
this.button3.Text = "读文件";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Items.AddRange(new object[] {
""});
this.listBox1.Location = new System.Drawing.Point(-2, 64);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(296, 208);
this.listBox1.TabIndex = 4;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(107, 12);
this.label1.TabIndex = 5;
this.label1.Text = "输入小于255的数:";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.label1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button3);
this.Controls.Add(this.butsave);
this.Controls.Add(this.butnext);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button butnext;
private System.Windows.Forms.Button butsave;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label1;
}
}
//手工添加代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace l_6_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(textBox1.Text);
FileStream fs = new FileStream(@"D:ga.txt", FileMode.Append);
BinaryWriter w = new BinaryWriter(fs);
w.Write(a);
textBox1.Clear();
w.Close();
}
private void button3_Click(object sender, EventArgs e)
{
FileInfo fileInfo = new FileInfo(@"D:ga.txt");
string[] a = new string[fileInfo.Length];
FileStream fs = new FileStream(@"D:ga.txt", FileMode.Open);
BinaryReader r = new BinaryReader(fs);
for (int i = 0; i < fileInfo.Length; i+=4)
{
a[i] = Convert.ToString(r.ReadInt32());
listBox1.Items.Add(a[i]);
}
r.Close();
}
private void butsave_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(textBox1.Text);
FileStream fs = new FileStream(@"D:ga.txt", FileMode.Append);
BinaryWriter w = new BinaryWriter(fs);
w.Write(a);
w.Close();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
namespace l_6_1
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.butnext = new System.Windows.Forms.Button();
this.butsave = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(137, 6);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 0;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// butnext
//
this.butnext.Location = new System.Drawing.Point(14, 33);
this.butnext.Name = "butnext";
this.butnext.Size = new System.Drawing.Size(76, 21);
this.butnext.TabIndex = 1;
this.butnext.Text = "输入下一个";
this.butnext.UseVisualStyleBackColor = true;
this.butnext.Click += new System.EventHandler(this.button1_Click);
//
// butsave
//
this.butsave.Location = new System.Drawing.Point(111, 32);
this.butsave.Name = "butsave";
this.butsave.Size = new System.Drawing.Size(76, 23);
this.butsave.TabIndex = 2;
this.butsave.Text = "存输入得数";
this.butsave.UseVisualStyleBackColor = true;
this.butsave.Click += new System.EventHandler(this.butsave_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(193, 33);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 3;
this.button3.Text = "读文件";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Items.AddRange(new object[] {
""});
this.listBox1.Location = new System.Drawing.Point(-2, 64);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(296, 208);
this.listBox1.TabIndex = 4;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(107, 12);
this.label1.TabIndex = 5;
this.label1.Text = "输入小于255的数:";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.label1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button3);
this.Controls.Add(this.butsave);
this.Controls.Add(this.butnext);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button butnext;
private System.Windows.Forms.Button butsave;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label1;
}
}
//手工添加代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace l_6_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(textBox1.Text);
FileStream fs = new FileStream(@"D:ga.txt", FileMode.Append);
BinaryWriter w = new BinaryWriter(fs);
w.Write(a);
textBox1.Clear();
w.Close();
}
private void button3_Click(object sender, EventArgs e)
{
FileInfo fileInfo = new FileInfo(@"D:ga.txt");
string[] a = new string[fileInfo.Length];
FileStream fs = new FileStream(@"D:ga.txt", FileMode.Open);
BinaryReader r = new BinaryReader(fs);
for (int i = 0; i < fileInfo.Length; i+=4)
{
a[i] = Convert.ToString(r.ReadInt32());
listBox1.Items.Add(a[i]);
}
r.Close();
}
private void butsave_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(textBox1.Text);
FileStream fs = new FileStream(@"D:ga.txt", FileMode.Append);
BinaryWriter w = new BinaryWriter(fs);
w.Write(a);
w.Close();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
- C# 中无法在流的结尾之外进行读取处理BinaryReader()
- 仿C# BinaryReader 读取二进制文件的Java实现代码
- C#使用BinaryReader类读取二进制文件
- C#使用BinaryReader类读取二进制文件
- C#使用BinaryReader类读取二进制文件
- c#二进制流的读和写(BinaryReader,BinaryWriter)
- 在C#中对于null/DBNull.Value值进行类型转换时,除了Convert.ToString()之外,其他的转换都会报空引用异常;
- 处理二进制文件(BinaryReader和BinaryWriter类 )
- 关于在虚拟机中无法读取usb设备的一种原因及处理方法
- 在Qt Widget中使用QPainter之外的引擎进行绘图
- 在Qt Widget中使用QPainter之外的引擎进行绘图
- unity C#中,用BinaryWrite和BinaryReader来存取数据
- C++中常见的文件读取结尾问题
- C++中常见的文件读取结尾问题
- C++中常见的文件读取结尾问题
- BinaryWriter和BinaryReader(二进制文件的读写)
- BinaryWriter和BinaryReader(二进制文件的读写)
- 在C#中进行声波的显示
- 23种常见设计模式的新解:泡妞实战篇
- 产品的价值是否就是满足用户需求?
- 程序员创业(一,准备过程)--创业未成功人士讲述未成功经验 - 椅子的专栏 - CSDNBlog
- 1042 W's Cipher
- sendmail安装
- C# 中无法在流的结尾之外进行读取处理BinaryReader()
- spring-jpetstore学习之(一)jpetstore 安装篇
- UNIX文件系统概述
- 新手.net 2.0提问)如何修改DataView的数据更新到数据源?
- CSS规则层叠时的优先级算法 - lunaticsun.com - CSDNBlog
- continuum1.1,svn,maven2 试用笔记
- ActiveDirectory学习日记(一)
- 6-用过一些JS-Tree控件之后的总结【将持续更新】
- 用sscanf格式化MAC地址