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(1376);
            
this.textBox1.Name = "textBox1";
            
this.textBox1.Size = new System.Drawing.Size(10021);
            
this.textBox1.TabIndex = 0;
            
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            
// 
            
// butnext
            
// 
            this.butnext.Location = new System.Drawing.Point(1433);
            
this.butnext.Name = "butnext";
            
this.butnext.Size = new System.Drawing.Size(7621);
            
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(11132);
            
this.butsave.Name = "butsave";
            
this.butsave.Size = new System.Drawing.Size(7623);
            
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(19333);
            
this.button3.Name = "button3";
            
this.button3.Size = new System.Drawing.Size(7523);
            
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(-264);
            
this.listBox1.Name = "listBox1";
            
this.listBox1.Size = new System.Drawing.Size(296208);
            
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(129);
            
this.label1.Name = "label1";
            
this.label1.Size = new System.Drawing.Size(10712);
            
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(292266);
            
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)
        {

        }

 


      
    }
}