当前位置:网站首页>C# 读写二进制文件
C# 读写二进制文件
2022-04-23 03:02:00 【dotNET跨平台】
读写二进制文件的一种选择是直接使用流类型;在这种情况下,最好使用字节数组执行读写操作。另一个选择是使用为这个场景定义的读取器和写入器:BinaryReader和BinaryWriter。使用它们的方式类似于使用 StreamReader 和 StreamWriter,但 BinaryReader 和 BinaryWriter 不使用任何编码。文件使用二进制格式而不是文本格式写入。
与 Stream 类型不同,BinaryWriter 为 Write() 方法定义了 18 个重载版本。重载版本接受不同的类型,如下面的代码片段所示,它写入 double、int、long 和string:
public static void WriteFileUsingBinaryWriter(string binFile)
{
var outputStream = File.Create(binFile);
using (var writer = new BinaryWriter(outputStream))
{
double d = 47.47;
int i = 42;
long 1 = 987654321;
string s = "sample";
writer.Write(d);
writer.Write(i);
writer.Write(1);
writer.Write(s)
}
}
为了再次读取文件,可以使用 BinaryReader。这个类定义的方法会读取所有不同的类型,如 ReadDouble、ReadInt32、ReadInt64 和 ReadString,如下所示:
public static void ReadFileUsingBinaryReader(string binFile)
{
var inputStream = File.Open(binFile, FileMode.Open);
using (var reader = new BinaryReader(inputStream))
{
double d = reader.ReadDouble();
int i = reader.ReadInt32();
long l = reader.ReadInt64();
string s = reader.ReadString();
Console.WriteLine($"d: {d}, i: {i},l: {1},s: {s}");
}
}
读取文件的顺序必须完全匹配写入的顺序。创建自己的二进制格式时,需要知道存储的内容和方式,并用相应的方式读取。旧的微软 Word 文档使用二进制文件格式,而新的 docx 文件扩展是 ZIP 文件。
微信公众号
Dotnet讲堂
版权声明
本文为[dotNET跨平台]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sd7o95o/article/details/124239610
边栏推荐
- 腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
- Microservices (distributed architecture)
- C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
- Navicat premium import SQL file
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
- Numpy stack function
- Processes and threads
- Liunx foundation - zabbix5 0 monitoring system installation and deployment
- Slave should be able to synchronize with the master in tests/integration/replication-psync. tcl
- L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
猜你喜欢
MAUI初体验:爽
Detailed explanation of distributed things
微软是如何解决 PC 端程序多开问题的
【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
Opencv fills the rectangle with a transparent color
Huawei machine test question -- deformation of hj53 Yang Hui triangle
Er and eer models
C# 11 的这个新特性,我愿称之最强!
FileNotFoundError: [Errno 2] No such file or directory
Slave should be able to synchronize with the master in tests/integration/replication-psync. tcl
随机推荐
Detailed explanation of distributed things
基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
Niuke white moon race 5 [problem solving mathematics field]
SQL statement - DDL
tf. keras. layers. Embedding function
Mosaic Routing: implement / home / news
Kubernetes study notes
Er and eer models
Android high-level interview must ask: overall business and project architecture design and reconstruction
Get together to watch (detailed version) eat a few cents a day
Microservices (distributed architecture)
Shell script learning notes - regular expressions
Response processing of openfeign
C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
《信息系统项目管理师总结》第四章 项目成本管理
.NET7之MiniAPI(特别篇):.NET7 Preview3
Centos7 install MySQL 8 0
[if you want to do a good job, you must first use its tools] Guide for downloading and using paper editing and document management (endnote, latex, jabref, overflow) resources
C# 11 的这个新特性,我愿称之最强!
Navicat premium import SQL file