当前位置:网站首页>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
边栏推荐
- ele之Table表格的封装
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (10)
- tf. keras. layers. Inputlayer function
- Shell script learning notes -- shell operation on files sed
- Slave should be able to synchronize with the master in tests/integration/replication-psync. tcl
- Kubernetes - detailed explanation of pod
- Configuring Apache Web services for servers such as Tianyi cloud
- Centos7 install MySQL 8 0
- ASP.NET和ASP.NETCore多环境配置对比
- 【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
猜你喜欢

Encapsulation of ele table

Airtrack cracking wireless network password (Dictionary running method)

微软是如何解决 PC 端程序多开问题的——内部实现
![FileNotFoundError: [Errno 2] No such file or directory](/img/ea/0c3f2768d14c1f4bb42bd1309ab996.png)
FileNotFoundError: [Errno 2] No such file or directory

C#中切片语法糖的使用

tf. keras. layers. MaxPooling? D function

It turns out that PID was born in the struggle between Lao wangtou and Lao sky

Plug in for vscode

Encapsulate components such as pull-down menu based on ele

Binary tree
随机推荐
Kubernetes - Introduction to actual combat
Deep q-network (dqn)
Specific field information of MySQL export table (detailed operation of Navicat client)
ASP.NET 6 中间件系列 - 自定义中间件类
最通俗易懂的依赖注入与控制反转
Error installing Mongo service 'mongodb server' on win10 failed to start
When using art template inheritance, compileerror: invalid or unexpected token generated
Xamarin效果第二十二篇之录音效果
Codeforces round 784 (Div. 4) (a - H)
Er and eer models
《信息系统项目管理师总结》第五章 项目质量管理
JS using the parameters of art template
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
[hcip] detailed explanation of six LSAS commonly used by OSPF
Dynamic sequence table + OJ
Openfeign service call
The express project changes the jade template to art template
Onenet connection process
JS learning notes
Array and collection types passed by openfeign parameters