C# 流與文件(Stream & File & byte[])


原文:https://www.cnblogs.com/long-gengyun/archive/2010/03/28/1698681.html

 

  • 文件概述 
文件在操作時表現為流,即流是從一些輸入中讀取到的一系列字節。

文件按信息在外部存儲器上按編碼方式可以分為文本文件二進制文件

 

Stream類是System.IO命名空間中的一個類,在System.IO命名空間中,包含所有允許在數據流和文件上進行同步和異步讀取和寫入的類,下面簡單介紹一下常用的類。

1. Directory類:包含了所有操作目錄的靜態方法,如目錄的創建,移動,復制,刪除等

2. DirectoryInfo類:包含了所有操作目錄的實例方法,如目錄的各種屬性(名字,創建時間),目錄的各種操作(目錄的創建,是否存在,移動,刪除等)

3. File類:是對文件的典型操作,提供文本的創建,打開,復制,刪除,移動等靜態方法。也可以用於獲取文件和設置文件的基本信息。

4. FileInfo類:對文件典型操作, 提供文本的創建,打開,復制,刪除,移動等實例方法。當文件需要多次重用時,使用FileInfo類提供的實例方法,不可以使用File提供的靜態方法。

5. FileStream類:該類實現了對文件讀取,寫入,打開,關閉操作,支持隨機訪問文件,可以使用同步方式打開文件進行讀寫,也可以用異步方式打開文件進行讀寫。

6. Path類:該類對包含文件或目錄路徑信息的String實例操作,這些操作可以跨平台的方式執行的。

7. MemoryStream類:該類創建起支持存儲區為內存的流。

8. StreamReader類:該類可以讀取標准文本文件的內容。即實現一個TextReader。默認編碼格式為UTF-8。

9. StreamWriter類:該類可以往標准文本文件中寫入內容。即實現一個TextWriter。默認編碼格式為UTF-8。

10. StringReader類: 該類實現從字符串進行讀取的TextReader。

11. StringWriter類:該類實現將信息寫入字符串,該信息存儲在基礎的StringBuilder中。

12. TextReader類:該類表示可讀取連續字符系統的閱讀器。

13. TextWriter類:該類表示可以編寫一個有序字符系列的編輯器,是抽象類。 

 

 代碼示例:文件的創建與文件內容的輸入輸出

 

if(File.Exists(filePath))
{
    File.Delete(filePath);
}
FileStream fs=File.Create(filePath,1024);     //創建文件
Byte[] info=new UTF8Encoding(true).GetBytes("測試內容");
fs.Write(info,0,info.Length);     //向新創建的文件寫入內容
fs.Close();

using(StreamReader sr=File.OpenText(filePath))
{
    While(sr.ReadLine()!=null)
        MessageBox.Show(sr.ReadLine());
}

 

參考代碼:

文件屬性的獲取與設置:/Files/long-gengyun/FileAttribute.rar

顯示文件夾中的文件與子文件夾:/Files/long-gengyun/GetDirectory.rar

文件的打印輸出:/Files/long-gengyun/FilePrint.rar

計算機系統信息獲取:/Files/long-gengyun/GetSystemInfo.rar 

 

一、byte[] 和 Stream

/// <summary>
/// byte[]轉換成Stream
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public Stream BytesToStream(byte[] bytes)
{
    Stream stream = new MemoryStream(bytes);
    return stream;
}
 
/// <summary>
/// Stream轉換成byte[]
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public byte[] StreamToBytes(Stream stream)
{
    byte[] bytes = new byte[stream.Length];
    stream.Read(bytes, 0, bytes.Length);
    stream.Seek(0, SeekOrigin.Begin); // 設置當前流的位置為流的開始
    return bytes;
}

二、文件 和 Stream

/// <summary>
/// 從文件讀取Stream,     思路=文件-字節-流
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public Stream FileToStream(string path)
{
    FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); // 打開文件
    byte[] bytes = new byte[fileStream.Length]; // 讀取文件的byte[]
    fileStream.Read(bytes, 0, bytes.Length);
    fileStream.Close();
    Stream stream = new MemoryStream(bytes); // 把byte[]轉換成Stream
    return stream;
}
 
/// <summary>
/// 將Stream寫入文件    思路=流-字節,文件-寫字節
/// </summary>
/// <param name="stream"></param>
/// <param name="path"></param>
public void StreamToFile(Stream stream, string path)
{
    byte[] bytes = new byte[stream.Length]; // 把Stream轉換成byte[]
    stream.Read(bytes, 0, bytes.Length);
    stream.Seek(0, SeekOrigin.Begin); // 設置當前流的位置為流的開始
    FileStream fs = new FileStream(path, FileMode.Create); // 把byte[]寫入文件
    BinaryWriter bw = new BinaryWriter(fs);
    bw.Write(bytes);
    bw.Close();
    fs.Close();
}

https://www.cnblogs.com/warioland/archive/2012/03/06/2381355.html

 

三、BYTE與string

1、
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
  byte[] inputBytes =converter.GetBytes(inputString);
  string inputString = converter.GetString(inputBytes);

2   string inputString = System.Convert.ToBase64String(inputBytes);
  byte[] inputBytes = System.Convert.FromBase64String(inputString);
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM