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 ...
随机推荐
- Nginx采用https加密访问后出现的问题
线上的一个网站运行了一段时间,应领导要求,将其访问方式更改为https加密方式.更改为https后,网站访问正常,但网站注册功能不能正常使用了! 经过排查,是nginx配置里结合php部分漏洞了一个参 ...
- Java连接Elasticsearch集群
package cn.test; import java.net.InetAddress; import java.net.UnknownHostException; import org.elast ...
- [转]iOS 应用内付费(IAP)开发步骤
FROM : http://blog.csdn.net/xiaoxiangzhu660810/article/details/17434907 参考文章链接: (1)http://mobile.51c ...
- Linux ssh登录和软件安装详解
阿哲Style Linux第一天 ssh登录和软件安装详解 Linux学习第一天 操作环境: Ubuntu 16.04 Win10系统,使用putty_V0.63 本身学习Linux就是想在服务器 ...
- SSH公钥认证+优化
一 ssh公钥认证流程: sshclinet机器:产生公私钥(公钥相当于一把锁) sshclient:将公钥发给sshserver(抛出锁子) sshclinet去连sshserver不需要密钥 ...
- [tools]google神器浏览器下载
google神器下载 这是一款优化了的google浏览器 http://www.ccav1.me/chromegae.html
- The ServiceClass object does not implement the required method in the following form: OMElement sayHello(OMElement e)
今天遇到一件诡异的事情,打好的同一个aar包,丢到测试环境tomcat,使用soapui测试,正常反馈结果. 丢到本地tomcat,使用soapui测试,始终报以下错误. <soapenv:En ...
- Quartz.net打造信息抽取器
由于最近的一个项目需要定时抽取特定XML信息,然后保存到数据库,最后通过WebApi把手机端要使用的方法给暴露出来,所以去研究了一下Quartz.net.由于项目很小,我没用到Autofac,Repo ...
- [CareerCup] 5.1 Insert Bits 插入位
5.1 You are given two 32-bit numbers, N and M, and two bit positions, land j. Write a method to inse ...
- 说说C#中IList与List区别
首先IList 泛型接口是 ICollection 泛型接口的子代,并且是所有泛型列表的基接口. 它仅仅是所有泛型类型的接口,并没有太多方法可以方便实用,如果仅仅是作为集合数据的承载体,确实,ILis ...