zoukankan
html css js c++ java
读取和写入二进制数据
在.NET平台,BinaryWriter 和
BinaryReader
类用于读取和写入二进制数据。
Code
1
using
System;
2
using
System.IO;
3
class
MyStream
4
{
5
private
const
string
FILE_NAME
=
"
Test.data
"
;
//
定义文件名
6
public
static
void
Main(String[] args)
7
{
8
//
检查是否文件已经存在
9
if
(File.Exists(FILE_NAME))
10
{
11
Console.WriteLine(
"
{0} already exists!
"
, FILE_NAME);
12
return
;
13
}
14
FileStream fs
=
new
FileStream(FILE_NAME, FileMode.CreateNew);
15
//
建立读写流
16
BinaryWriter w
=
new
BinaryWriter(fs);
17
//
写入测试数据
18
for
(
int
i
=
0
; i
<
11
; i
++
)
19
{
20
w.Write( (
int
) i);
21
}
22
w.Close();
23
fs.Close();
24
//
建立读取类.
25
fs
=
new
FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
26
BinaryReader r
=
new
BinaryReader(fs);
27
28
29
//
读取测试数据
30
31
for
(
int
i
=
0
; i
<
11
; i
++
)
32
{
33
Console.WriteLine(r.ReadInt32());
34
}
35
r.Close();
36
fs.Close();
37
}
38
}
39
40
上面的代码示例演示如何向新的空文件流 (
Test.data
) 写入数据及从中读取数据。
查看全文
相关阅读:
Bayan 2015 Contest Warm Up D. CGCDSSQ 暴力
Codeforces Round #361 (Div. 2) D. Friends and Subsequences RMQ+二分
Educational Codeforces Round 21 D. Array Division 前缀和
Educational Codeforces Round 23 E. Choosing The Commander Trie
Educational Codeforces Round 23 D. Imbalanced Array 单调栈
Codeforces Round #421 (Div. 1) B. Mister B and PR Shifts 模拟
Educational Codeforces Round 24 E. Card Game Again 二分+线段树
Educational Codeforces Round 25 E. Minimal Labels 优先队列
Codeforces Round #426 (Div. 1) B. The Bakery DP+线段树
Codeforces Round #407 (Div. 1) C. The Great Mixing 背包DP+Bitset
原文地址:https://www.cnblogs.com/CCJVL/p/1355382.html
最新文章
coding++:漫画版-了解什么是分布式事务?
coding++:Dubbo的介绍和架构
coding++:springboot 多线程@Async
coding++:java 线程池概述
coding++:MySQL 数据库常见两种引擎
Ali_Cloud++:阿里云部署 Jenkins持续集成自动化部署
Ali_Cloud++:阿里云-单机版 solr4.10.3 安装部署
redis++:Redis持久化 rdb & aof 工作原理及流程图 (三)
redis++:Redis持久化中 rdb 备份策略中的配置参数
redis++:Redis持久化中 aof 备份策略中的配置参数
热门文章
文件操作总结(1)
集合操作总结
用python编写购物车程序(增加卖家入口,用户信息保存)
利用字典编写菜单程序
字符串操作总结
浅copy与深copy
用python编写购物程序(2)
用python编写购物程序(1)
用Python编写登录接口
Codeforces Round #104 (Div. 1) E. Lucky Queries 线段树
Copyright © 2011-2022 走看看