C#二进制读写BinaryReader、BinaryWriter、BinaryFormatter
作者:springsnow 时间:2022-03-07 23:01:28
一、二进制读写类:
1、BinaryReader/BinaryWriter:二进制读写
BinaryReader:用特定的编码将基元数据类型读作二进制值。
BinaryWriter:以二进制形式将基元类型写入流,并支持用特定的编码写入字符串。
2、XmlReader/XmlWriter :XML读写
见:C#使⽤XmlReader和XmlWriter操作XML⽂件
二、BinaryReader/BinaryWriter
读写流的基元数据类型。可以操作图像、压缩文件等二进制文件。也可以是MemoryStream等。
不需要一个字节一个字节进行操作,可以是2个、4个、或8个字节这样操作。
可以将一个字符或数字按指定数量的字节进行写入。
1、写入:
using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
writer.Write(1.250F);
writer.Write(@"c:\Temp");
writer.Write(10);
writer.Write(true);
}
Response.BinaryWrite()方法输出二进制图像
FileStream fs = new FileStream(Server.MapPath("未命名.jpg"), FileMode.Open);//将图片文件存在文件流中
long fslength = fs.Length;//流长度
byte[] b=new byte[(int)fslength];//定义二进制数组
fs.Read(b, 0, (int)fslength);//将流中字节写入二进制数组中
fs.Close();//关闭流
Response.ContentType = "image/jpg";//没有这个会出现乱码
Response.BinaryWrite(b);//将图片输出在页面
2、读取:
每次读取都回提升流中的当前位置相应数量的字节。
下面的代码示例演示了如何存储和检索文件中的应用程序设置。
const string fileName = "AppSettings.dat";
float aspectRatio;
string tempDirectory;
int autoSaveTime;
bool showStatusBar;
if (File.Exists(fileName))
{
using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
{
aspectRatio = reader.ReadSingle();
tempDirectory = reader.ReadString();
autoSaveTime = reader.ReadInt32();
showStatusBar = reader.ReadBoolean();
}
Console.WriteLine("Aspect ratio set to: " + aspectRatio);
Console.WriteLine("Temp directory is: " + tempDirectory);
Console.WriteLine("Auto save time set to: " + autoSaveTime);
Console.WriteLine("Show status bar: " + showStatusBar);
}
BinaryReader读取图片:
using (FileStream fs = new FileStream("1.jpg", FileMode.Open, FileAccess.Read))
{
//将图片以文件流的形式进行保存
using (BinaryReader br = new BinaryReader(fs))
{
byte[] imgBytesIn = br.ReadBytes((int)fs.Length); //将流读入到字节数组中
br.Close();
}
}
三、以二进制格式序列化对象BinaryFormatter
1、SoapFormatter(用于HTTP中)和BinaryFormatter(用于TCP中)类实现了IFormatter接口 (由继承IRemotingFormatter,支持远程过程调用 (Rpc))
Deserialize(Stream) 反序列化所提供流中的数据并重新组成对象图形。
Serialize(Stream, Object) 将对象或具有给定根的对象图形序列化为所提供的流。
XML序列化见:https://www.jb51.net/article/250477.htm
2、举例:
[Serializable]
public class Product //实体类
{
public long Id;
[NonSerialized]//标识不序列化此成员Name
public string Name;
public Product(long Id, string Name)
{
this.Id = Id;
this.Name = Name;
}
}
static void Main()
{
//序列化(对象保存到文件)
List<Product> Products = new List<Product> {
new Product(1,"a"),new Product(2,"b")
};
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, Products);
fs.Close();
//反序列化(文件内容转成对象)
FileStream fs1 = new FileStream("DataFile.dat", FileMode.Open);
BinaryFormatter formatter1 = new BinaryFormatter();
List<Product> addresses = (List<Product>)formatter1.Deserialize(fs1);
fs1.Close();
foreach (Product de in addresses)
{
Console.WriteLine("{0} lives at {1}.", de.Id, de.Name);
}
}
来源:https://www.cnblogs.com/springsnow/p/13560446.html
标签:C#,二进制,读写,BinaryReader,BinaryWriter,BinaryFormatter
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java数组传递及可变参数操作实例详解
2023-08-26 21:21:59
![](https://img.aspxhome.com/file/2023/0/77160_0s.png)
Spring Boot详解配置文件的用途与用法
2022-08-01 20:37:27
![](https://img.aspxhome.com/file/2023/4/77954_0s.png)
C#语言主要语言区域
2021-10-05 13:58:38
JAVA版排序算法之快速排序示例
2023-04-20 04:37:53
使用okhttp替换Feign默认Client的操作
2021-10-03 16:57:59
![](https://img.aspxhome.com/file/2023/6/70536_0s.jpg)
解决idea爆红 cant resolve symbol String的问题解析
2023-10-11 10:47:30
![](https://img.aspxhome.com/file/2023/5/58615_0s.png)
java Springboot实现教务管理系统
2023-01-18 00:28:12
![](https://img.aspxhome.com/file/2023/6/64906_0s.png)
SpringBoot如何进行对象复制的实践
2023-11-23 03:40:19
![](https://img.aspxhome.com/file/2023/5/59935_0s.jpg)
C#中List集合使用Max()方法查找到最大值的实例
2023-12-01 08:03:32
SSH原理及两种登录方法图文详解
2023-11-14 11:10:53
![](https://img.aspxhome.com/file/2023/8/59038_0s.png)
java基础二叉搜索树图文详解
2023-06-01 05:43:19
![](https://img.aspxhome.com/file/2023/6/61016_0s.png)
java集合迭代器Iterator中的remove陷阱
2022-06-17 16:11:54
基于Java的MathML转图片的方法(示例代码)
2023-11-25 14:29:39
![](https://img.aspxhome.com/file/2023/3/76503_0s.png)
浅谈Java中ArrayList线程不安全怎么办
2023-10-02 19:24:56
10道springboot常见面试题
2023-09-02 03:02:22
浅谈JAVA设计模式之享元模式
2021-09-22 10:12:06
![](https://img.aspxhome.com/file/2023/2/66062_0s.jpg)
解决idea中yml文件不识别的问题
2021-12-07 02:09:28
![](https://img.aspxhome.com/file/2023/7/78347_0s.jpg)
hadoop运行java程序(jar包)并运行时动态指定参数
2023-07-27 11:02:10
![](https://img.aspxhome.com/file/2023/9/57829_0s.png)
JAVA实现社会统一信用代码校验的方法
2023-04-10 08:38:21
Java实现聊天室界面
2023-12-15 10:43:32