sing 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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Random r = new Random();
            byte[] writeArray = new byte[5];
            r.NextBytes(writeArray);
            for (int k = 0; k < 5; k++)
            {
                textBox1.Text += writeArray[k].ToString() + " ";
            }
            // MessageBox.Show("控件已经激活");

            if (textBox1.Text == string.Empty)
            {
                MessageBox.Show("要写入的文件内容不能为空");
            }
            else
            {
                SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
                SaveFileDialog1.Filter = "二进制文件(*.dat)|*.dat";
                if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    FileStream myStream = new FileStream(SaveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    BinaryWriter myWriter = new BinaryWriter(myStream);
                    myWriter.Write(textBox1.Text);
                    myWriter.Close();
                    myStream.Close();
                    textBox1.Text = string.Empty;
                }
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {

            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "二进制文件(*.dat)|*.dat";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = string.Empty;
                FileStream myStream = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                BinaryReader myReader = new BinaryReader(myStream);
                if (myReader.PeekChar() != -1)
                {
                    textBox1.Text = Convert.ToString(myReader.ReadInt32());
                }
                myReader.Close();
                myStream.Close();
            }
        }
    }
}

C#二进制文件的读写的更多相关文章

  1. C++学习49 对二进制文件的读写操作

    二进制文件不是以ASCII代码存放数据的,它将内存中数据存储形式不加转换地传送到磁盘文件,因此它又称为内存数据的映像文件.因为文件中的信息不是字符数据,而是字节中的二进制形式的信息,因此它又称为字节文 ...

  2. 从零开始学C++之IO流类库(三):文件的读写、二进制文件的读写、文件随机读写

    一.文件的读写 如前面所提,流的读写主要有<<, >>, get, put, read, write 等操作,ofstream 继承自ostream, ifstream 继承自 ...

  3. 简单Java程序向实用程序的过度:二进制文件的读写

    File I/O中常见的文件读写: 1.字节流读写文本文件 FileInputStream; FileOutputStream; 2.字符流读写文本文件 FileReader; FileWriter; ...

  4. 原 BinaryWriter和BinaryReader(二进制文件的读写)

    原文 BinaryWriter和BinaryReader(二进制文件的读写) C#的FileStream类提供了最原始的字节级上的文件读写功能,但我们习惯于对字符串操作,于是StreamWriter和 ...

  5. C++入门到理解之文件操作(文本文件的读写+二进制文件的读写)

    原文地址http://www.javayihao.top/detail/168 一:概述 1.程序在运行中产生的数据都是临时数据,程序一旦运行结束会被释放,可以通过文件相关的操作将数据持久保存. 2. ...

  6. cocos2d-x 二进制文件的读写

    转自:http://blog.csdn.net/wolfking_2009/article/details/10616069 cocos2d-x里面的二进制文件读取的方法是有的,作者对方法封装了下,将 ...

  7. open语句对文本和二进制文件的读写

    文本文件的操作此种方式是以行为单位进行读取的基本单位,主要应用的方法和函数有Open,Close,Line Input,FreeFile,EOF等.先简述其功能然后结合代码示例进行说明.Open:顾名 ...

  8. C++二进制文件中读写bitset

    这个比较简单,直接上代码: bitset< > *b = >(); bitset< > *c = >(); ofstream out("I:\\test. ...

  9. 将日期和时间作为 struct tm型的值直接向二进制文件进行读写

    #include <stdio.h> #include <time.h> char data_file[]="D:\\%\\datetime.dat"; v ...

随机推荐

  1. Converting a Polygon ZM shape file to a regular Shape Polygon

    from:http://blog.csdn.net/qb371/article/details/8102109 Locate the following tool - ArcToolbox > ...

  2. javascript中的迭代器

    1.forEach迭代器 forEach方法接收一个函数作为参数,对数组中每个元素使用这个函数,只调用这个函数,数组本身没有任何变化 //forEach迭代器 function square(num) ...

  3. VFS分析(一)挂载(持续更新)

    基础知识在<深入linux内核架构>第8章,自行脑补. 看下几个关键的过程: do_add_mount里有重要函数lock_mount, lock_mount函数的输入是struct pa ...

  4. f2fs解析(八)node 管理器中的node_info

    free_info 功成身退,node_info顺利接班. // 这里还是蛮复杂的一件事,如果不搞清除的话,这个历史性的接班工作我们就接不上 上面说到 alloc_nid 和 alloc_nid_do ...

  5. VS的代码分析工具

    来自:[译]Visual Studio 2008 Code Metrics http://www.cnblogs.com/live41/archive/2010/02/08/1665627.html ...

  6. 一个DOM元素绑定多个事件时,先执行冒泡还是捕获

    绑定在被点击元素的事件是按照代码顺序发生,其他元素通过冒泡或者捕获“感知”的事件,按照W3C的标准,先发生捕获事件,后发生冒泡事件.所有事件的顺序是:其他元素捕获阶段事件 -> 本元素代码顺序事 ...

  7. winform listbox与textbox组合提示框 模糊查询

      private void listbox1_MouseClick(object sender, MouseEventArgs e)        {            textbox1.Vis ...

  8. C# 与 Unity 同名函数

    1,Random,直接使用Random会报错,要么使用UnityEngine.Random,要么使用System.Random

  9. 设计模式——1.概述&UML类图和时序图

    声明:本博客设计模式相关文章均整理和修改自网络,原文地址:图说设计模式 学习设计模式的3个层次—— 1.熟悉所有设计模式: 2.能够用代码实现: 3.运用到工作的项目中. 设计模式指导软件开发,学习设 ...

  10. Tomcat中部署后JspFactory报异常的解决方案

    The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory的异常的原因及解决办法原 ...