C#二进制文件的读写
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#二进制文件的读写的更多相关文章
- C++学习49 对二进制文件的读写操作
二进制文件不是以ASCII代码存放数据的,它将内存中数据存储形式不加转换地传送到磁盘文件,因此它又称为内存数据的映像文件.因为文件中的信息不是字符数据,而是字节中的二进制形式的信息,因此它又称为字节文 ...
- 从零开始学C++之IO流类库(三):文件的读写、二进制文件的读写、文件随机读写
一.文件的读写 如前面所提,流的读写主要有<<, >>, get, put, read, write 等操作,ofstream 继承自ostream, ifstream 继承自 ...
- 简单Java程序向实用程序的过度:二进制文件的读写
File I/O中常见的文件读写: 1.字节流读写文本文件 FileInputStream; FileOutputStream; 2.字符流读写文本文件 FileReader; FileWriter; ...
- 原 BinaryWriter和BinaryReader(二进制文件的读写)
原文 BinaryWriter和BinaryReader(二进制文件的读写) C#的FileStream类提供了最原始的字节级上的文件读写功能,但我们习惯于对字符串操作,于是StreamWriter和 ...
- C++入门到理解之文件操作(文本文件的读写+二进制文件的读写)
原文地址http://www.javayihao.top/detail/168 一:概述 1.程序在运行中产生的数据都是临时数据,程序一旦运行结束会被释放,可以通过文件相关的操作将数据持久保存. 2. ...
- cocos2d-x 二进制文件的读写
转自:http://blog.csdn.net/wolfking_2009/article/details/10616069 cocos2d-x里面的二进制文件读取的方法是有的,作者对方法封装了下,将 ...
- open语句对文本和二进制文件的读写
文本文件的操作此种方式是以行为单位进行读取的基本单位,主要应用的方法和函数有Open,Close,Line Input,FreeFile,EOF等.先简述其功能然后结合代码示例进行说明.Open:顾名 ...
- C++二进制文件中读写bitset
这个比较简单,直接上代码: bitset< > *b = >(); bitset< > *c = >(); ofstream out("I:\\test. ...
- 将日期和时间作为 struct tm型的值直接向二进制文件进行读写
#include <stdio.h> #include <time.h> char data_file[]="D:\\%\\datetime.dat"; v ...
随机推荐
- esc设置多站点 域名解析
你的域名是:a.com IP: 1.1.1.1 一 设置域名解析: 1.a.com 解析到 1.1.1.1 2.a.com 解析到 2.2.2.2 LOOP 二 到服务器上: 在1站点设置主机 ...
- Gerrit日常操作命令收集
Gerrit代码审核工具是个好东西,尤其是在和Gitlab和Jenkins对接后,在代码控制方面有着无与伦比的优势. 在公司线上部署了一套Gerrit系统,在日常运维中,使用了很多gerrit命令,在 ...
- javascript中的hasOwnProperty和isPrototypeOf
hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象.不过需要注意的是,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员.isPrototypeOf ...
- StackBlur.js
StackBlur.js 是 Mario Klingemann 创建的一个快速的.接近高斯模糊的效果库. 更多信息: http://incubator.quasimondo.com/processin ...
- poj 1411 Calling Extraterrestrial Intelligence Again(超时)
Calling Extraterrestrial Intelligence Again Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- usb驱动开发7之接口描述符
前面struct usb_interface里表示接口设置的struct usb_host_interface被有意的飘过了,咱们在这节主要讲讲这个结构体,同样在include/linux/usb.h ...
- Linux Linux程序练习十二(select实现QQ群聊)
//头文件--helper.h #ifndef _vzhang #define _vzhang #ifdef __cplusplus extern "C" { #endif #de ...
- “插件(application/x-vlc-plugin)不受支持”NPAPI和PPAPI的问题
“插件(application/x-vlc-plugin)不受支持”NPAPI和PPAPI的问题 最近做一个前端的项目,项目需要引用VLC浏览器插件,javascript在IE.Firefox等浏览器 ...
- python数字图像处理(2):图像的读取、显示与保存
skimage提供了io模块,顾名思义,这个模块是用来图片输入输出操作的.为了方便练习,也提供一个data模块,里面嵌套了一些示例图片,我们可以直接使用. 引入skimage模块可用: from sk ...
- 流程引擎Activiti系列:在eclipse中搭建咖啡兔的Activiti演示工程中的各种坑及其解决方法(kft-activiti-demo-no-maven)
近期在学习activiti,打算基于现有的框架,比如activiti-explorer或者咖啡兔的示例工程 kft-activiti-demo,在此基础上添加自己的业务流程,看看是否可以走通,以及这个 ...