最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#二進(jìn)制讀寫類怎么使用

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

創(chuàng)新互聯(lián)專注于塔什庫爾干塔吉克網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供塔什庫爾干塔吉克營銷型網(wǎng)站建設(shè),塔什庫爾干塔吉克網(wǎng)站制作、塔什庫爾干塔吉克網(wǎng)頁設(shè)計、塔什庫爾干塔吉克網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造塔什庫爾干塔吉克網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供塔什庫爾干塔吉克網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

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

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

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

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

二、BinaryReader/BinaryWriter

讀寫流的基元數(shù)據(jù)類型。可以操作圖像、壓縮文件等二進(jìn)制文件。也可以是MemoryStream等。

不需要一個字節(jié)一個字節(jié)進(jìn)行操作,可以是2個、4個、或8個字節(jié)這樣操作。

可以將一個字符或數(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";//沒有這個會出現(xiàn)亂碼
Response.BinaryWrite(b);//將圖片輸出在頁面

2、讀?。?/h4>

每次讀取都回提升流中的當(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中)類實現(xiàn)了IFormatter接口 (由繼承IRemotingFormatter,支持遠(yuǎn)程過程調(diào)用 (Rpc))

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

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

2、舉例:

[Serializable]
public class Product //實體類
{
    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 Products = new List {
        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 addresses = (List)formatter1.Deserialize(fs1);
    fs1.Close();
    foreach (Product de in addresses)
    {
        Console.WriteLine("{0} lives at {1}.", de.Id, de.Name);
    }
}

“C#二進(jìn)制讀寫類怎么使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!


當(dāng)前題目:C#二進(jìn)制讀寫類怎么使用
轉(zhuǎn)載來于:http://fisionsoft.com.cn/article/gscsso.html