溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么使用C#二進(jìn)制讀寫B(tài)inaryReader、BinaryWriter、BinaryFormatter

發(fā)布時(shí)間:2022-06-15 09:24:16 來(lái)源:億速云 閱讀:193 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容介紹了“怎么使用C#二進(jìn)制讀寫B(tài)inaryReader、BinaryWriter、BinaryFormatter”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

一、二進(jìn)制讀寫類:

1、BinaryReader/BinaryWriter:二進(jìn)制讀寫

  • BinaryReader:用特定的編碼將基元數(shù)據(jù)類型讀作二進(jìn)制值。

  • BinaryWriter:以二進(jìn)制形式將基元類型寫入流,并支持用特定的編碼寫入字符串。

2、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;//流長(zhǎng)度
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";//沒(méi)有這個(gè)會(huì)出現(xiàn)亂碼
Response.BinaryWrite(b);//將圖片輸出在頁(yè)面

2、讀?。?/h4>

每次讀取都回提升流中的當(dāng)前位置相應(yīng)數(shù)量的字節(jié)。

下面的代碼示例演示了如何存儲(chǔ)和檢索文件中的應(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)制格式序列化對(duì)象BinaryFormatter

1、SoapFormatter(用于HTTP中)和BinaryFormatter(用于TCP中)類實(shí)現(xiàn)了IFormatter接口 (由繼承IRemotingFormatter,支持遠(yuǎn)程過(guò)程調(diào)用 (Rpc))

  • Deserialize(Stream) 反序列化所提供流中的數(shù)據(jù)并重新組成對(duì)象圖形。

  • Serialize(Stream, Object) 將對(duì)象或具有給定根的對(duì)象圖形序列化為所提供的流。

2、舉例:

[Serializable]
public class Product //實(shí)體類
{
    public long Id;
    [NonSerialized]//標(biāo)識(shí)不序列化此成員Name
    public string Name;
    public Product(long Id, string Name)
    {
        this.Id = Id;
        this.Name = Name;
    }
}

static void Main()
{
    //序列化(對(duì)象保存到文件)
    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)成對(duì)象)
    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);
    }
}

“怎么使用C#二進(jìn)制讀寫B(tài)inaryReader、BinaryWriter、BinaryFormatter”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI