C#二進制讀寫B(tài)inaryReader、BinaryWriter、BinaryFormatter
一、二進制讀寫類:
1、BinaryReader/BinaryWriter:二進制讀寫
- BinaryReader:用特定的編碼將基元數(shù)據(jù)類型讀作二進制值。
- BinaryWriter:以二進制形式將基元類型寫入流,并支持用特定的編碼寫入字符串。
2、XmlReader/XmlWriter :XML讀寫
見:C#使?XmlReader和XmlWriter操作XML?件
二、BinaryReader/BinaryWriter
讀寫流的基元數(shù)據(jù)類型。可以操作圖像、壓縮文件等二進制文件。也可以是MemoryStream等。
不需要一個字節(jié)一個字節(jié)進行操作,可以是2個、4個、或8個字節(jié)這樣操作。
可以將一個字符或數(shù)字按指定數(shù)量的字節(jié)進行寫入。
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];//定義二進制數(shù)組 fs.Read(b, 0, (int)fslength);//將流中字節(jié)寫入二進制數(shù)組中 fs.Close();//關閉流 Response.ContentType = "image/jpg";//沒有這個會出現(xiàn)亂碼 Response.BinaryWrite(b);//將圖片輸出在頁面
2、讀?。?/h3>
每次讀取都回提升流中的當前位置相應數(shù)量的字節(jié)。
下面的代碼示例演示了如何存儲和檢索文件中的應用程序設置。
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); //將流讀入到字節(jié)數(shù)組中 br.Close(); } }
三、以二進制格式序列化對象BinaryFormatter
1、SoapFormatter(用于HTTP中)和BinaryFormatter(用于TCP中)類實現(xiàn)了IFormatter接口 (由繼承IRemotingFormatter,支持遠程過程調(diào)用 (Rpc))
- Deserialize(Stream) 反序列化所提供流中的數(shù)據(jù)并重新組成對象圖形。
- Serialize(Stream, Object) 將對象或具有給定根的對象圖形序列化為所提供的流。
XML序列化見:http://www.dbjr.com.cn/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(); //反序列化(文件內(nèi)容轉成對象) 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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
c#中SqlHelper封裝SqlDataReader的方法
這篇文章主要介紹了c#中SqlHelper封裝SqlDataReader的方法,涉及C#針對數(shù)據(jù)庫相關操作封裝與使用的技巧,需要的朋友可以參考下2015-05-05C# 實現(xiàn)的圖片蓋章功能,支持拖拽、旋轉、放縮、保存
這篇文章主要介紹了C# 實現(xiàn)的圖片蓋章功能,支持拖拽、旋轉、放縮、保存,需要的朋友可以參考下2014-04-04