C#二進(jìn)制讀寫B(tài)inaryReader、BinaryWriter、BinaryFormatter
一、二進(jìn)制讀寫類:
1、BinaryReader/BinaryWriter:二進(jìn)制讀寫
- BinaryReader:用特定的編碼將基元數(shù)據(jù)類型讀作二進(jìn)制值。
- BinaryWriter:以二進(jìn)制形式將基元類型寫入流,并支持用特定的編碼寫入字符串。
2、XmlReader/XmlWriter :XML讀寫
見:C#使?XmlReader和XmlWriter操作XML?件
二、BinaryReader/BinaryWriter
讀寫流的基元數(shù)據(jù)類型??梢圆僮鲌D像、壓縮文件等二進(jìn)制文件。也可以是MemoryStream等。
不需要一個(gè)字節(jié)一個(gè)字節(jié)進(jìn)行操作,可以是2個(gè)、4個(gè)、或8個(gè)字節(jié)這樣操作。
可以將一個(gè)字符或數(shù)字按指定數(shù)量的字節(jié)進(jìn)行寫入。
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()方法輸出二進(jìn)制圖像
FileStream fs = new FileStream(Server.MapPath("未命名.jpg"), FileMode.Open);//將圖片文件存在文件流中
long fslength = fs.Length;//流長度
byte[] b=new byte[(int)fslength];//定義二進(jìn)制數(shù)組
fs.Read(b, 0, (int)fslength);//將流中字節(jié)寫入二進(jìn)制數(shù)組中
fs.Close();//關(guān)閉流
Response.ContentType = "image/jpg";//沒有這個(gè)會(huì)出現(xiàn)亂碼
Response.BinaryWrite(b);//將圖片輸出在頁面2、讀?。?/h3>
每次讀取都回提升流中的當(dāng)前位置相應(yīng)數(shù)量的字節(jié)。
下面的代碼示例演示了如何存儲和檢索文件中的應(yīng)用程序設(shè)置。
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))
{
//將圖片以文件流的形式進(jìn)行保存
using (BinaryReader br = new BinaryReader(fs))
{
byte[] imgBytesIn = br.ReadBytes((int)fs.Length); //將流讀入到字節(jié)數(shù)組中
br.Close();
}
}三、以二進(jìn)制格式序列化對象BinaryFormatter
1、SoapFormatter(用于HTTP中)和BinaryFormatter(用于TCP中)類實(shí)現(xiàn)了IFormatter接口 (由繼承IRemotingFormatter,支持遠(yuǎn)程過程調(diào)用 (Rpc))
- Deserialize(Stream) 反序列化所提供流中的數(shù)據(jù)并重新組成對象圖形。
- Serialize(Stream, Object) 將對象或具有給定根的對象圖形序列化為所提供的流。
XML序列化見:http://www.dhdzp.com/article/250477.htm
2、舉例:
[Serializable]
public class Product //實(shí)體類
{
public long Id;
[NonSerialized]//標(biāo)識不序列化此成員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();
//反序列化(文件內(nèi)容轉(zhuǎn)成對象)
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);
}
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C# BinaryReader實(shí)現(xiàn)讀取二進(jìn)制文件
- C# 進(jìn)制轉(zhuǎn)換的實(shí)現(xiàn)(二進(jìn)制、十六進(jìn)制、十進(jìn)制互轉(zhuǎn))
- C#中圖片、二進(jìn)制與字符串的相互轉(zhuǎn)換方法
- C#實(shí)現(xiàn)文件與二進(jìn)制互轉(zhuǎn)并存入數(shù)據(jù)庫
- C#讀取二進(jìn)制文件方法分析
- c#二進(jìn)制逆序方法詳解
- C# 圖片與二進(jìn)制轉(zhuǎn)換的簡單實(shí)例
- C# 向二進(jìn)制文件進(jìn)行讀寫的操作方法
- c# 以二進(jìn)制讀取文本文件
相關(guān)文章
C# 獲取進(jìn)程退出代碼的實(shí)現(xiàn)示例
這篇文章主要介紹了C# 獲取進(jìn)程退出代碼的實(shí)現(xiàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
C#實(shí)現(xiàn)Array,List,Dictionary相互轉(zhuǎn)換
這篇文章介紹了C#實(shí)現(xiàn)Array,List,Dictionary互相轉(zhuǎn)換的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
利用C#實(shí)現(xiàn)獲取與監(jiān)控電腦系統(tǒng)信息
在C#中,獲取與監(jiān)控電腦系統(tǒng)信息通常可以通過多種方式實(shí)現(xiàn),這篇文章主要為大家整理了幾種常見的方法及其示例代碼,希望對大家有所幫助2024-11-11
基于C#實(shí)現(xiàn)任意格式JSON文本的HTTP交互抽象類
為了實(shí)現(xiàn)一個(gè)支持任意格式JSON交互的抽象類,并且在整個(gè)過程中不需要對JSON格式數(shù)據(jù)進(jìn)行序列化和反序列化操作,可以使用C#中的HttpClient類來進(jìn)行HTTP請求和響應(yīng),本文給大家介紹了基于C#實(shí)現(xiàn)任意格式JSON文本的HTTP交互抽象類,需要的朋友可以參考下2025-03-03

