C#的字節與流


     計算機中文件有很多種,我們知道實際存在計算機中的都是二進制。這里我記錄了通過流對文件的讀取操作。

     一、首先在這里簡單涉及下位,字節,字符的概念。

     位(bit):可以表示0或1;

     字節(byte):由8位組成(bit),可以表示0-255,是256個不同的數據;

     字符:字符根據編碼的不同有所區別;

     ANSI編碼(本地化):它是支持本地的編碼方式,不同 ANSI 編碼之間互不兼容。在簡體中文系統下,ANSI 編碼代表 GB2312 編碼,在日文操作系統下,ANSI 編碼代表 JIS 編碼。對於字符來說ANSI以單字節存放英文字符,以雙字節存放中文等字符。

     Unicoide編碼:Unicode下,英文和中文的字符都以雙字節存放。用來給 UNICODE 字符集編碼的標准有很多種,比如:UTF-8, UTF-7, UTF-16, UnicodeLittle, UnicodeBig 等。

     UTF-8:是表示一個字符是可變的,有可能是用一個字節表示一個字符,也可能是兩個,三個。當然最多不能超過3個字節了。反正是根據字符對應的數字大小來確定。

     UTF-16:任何字符對應的數字都用兩個字節來保存。

 

     二、C# Stream流的主要用途就是與應用程序外部的文件或者數據源進行數據交互。

     有文件流FileStream,網絡流NetwrokStream,訪問網絡時InputStream,OutputStream等。流的內部就是對二進制數據的讀取。

     流可以一次性讀取,也可以循環讀取。

     一次性讀取:

 1 public void Read()  2 {  3     string filePath = Environment.CurrentDirectory + "/content.txt";  4     Stream source = new FileStream(filePath, FileMode.Open, FileAccess.Read);  5 
 6     byte[] buffer = new byte[source.Length];  7     int bytesRead = source.Read(buffer, 0, (int)source.Length);  8 
 9  Output(buffer); 10 } 

     循環讀取:

 public void ReadLoop() 2 { 3 string filePath = Environment.CurrentDirectory + "/content.txt"; 4 string fileDest = Environment.CurrentDirectory + "/dest.txt"; 5 Stream source = new FileStream(filePath, FileMode.Open, FileAccess.Read); 6 Stream destStream = new FileStream(fileDest, FileMode.Create, FileAccess.Write); 7 8 int bufferSize = 10; 9 byte[] buffer = new byte[bufferSize]; 10 int bytesREad; 11 while((bytesREad= source.Read(buffer, 0, bufferSize)>0)) { destStream.Write(buffer, 0, bytesREad); } 18 source.Dispose(); 19 destStream.Dispose(); 20 } 

     StreamReader, StreamWriter。StringReader, StringWriter。它們是流的包裝器類,方便對流的讀取。以下是示例代碼:

1 public void StreamReaderRead() 2 { 3     string filePath = Environment.CurrentDirectory + "/content.txt"; 4     Stream source = new FileStream(filePath, FileMode.Open, FileAccess.Read); 5     StreamReader sr = new StreamReader(source);//Stream包裝器類 
6     string text = sr.ReadToEnd(); 7 
8  Console.Write(text); 9 } 
 1 public void StreamWriterWrite()  2 {  3     string filePath = Environment.CurrentDirectory + "/content.txt";  4     string fileDest = Environment.CurrentDirectory + "/dest1.txt";  5       
 6     Stream source = new FileStream(filePath, FileMode.Open, FileAccess.Read);  7 
 8     StreamReader sr = new StreamReader(source);//Stream包裝器類 
 9     string text = sr.ReadToEnd(); 10 
11     StreamWriter sw = new StreamWriter(fileDest);//Stream包裝器類 
12  sw.Write(text); 13  sw.Flush(); 14  sw.Close(); 15 } 

 

     三、二進制字節流讀寫封裝

     完成以下功能:

  • 只針對內存字節流的讀寫,主要應用於數據的解析和寫入。
  • 提供不同數據類型的讀寫接口,包括byte,short,int,float,string等。
  • 處理了大小端數據轉換的問題,所以可用於網絡數據的解析和發送。
  1 using System.IO; 
  2 using System.Net;
  3 using System;
  4 
  5 namespace Framework
  6 {
  7     public class NetStream
  8     {
  9         private MemoryStream stream;
 10         private BinaryReader reader;
 11         private BinaryWriter writer;
 12 
 13         public NetStream(byte[] buffer = null)
 14         {
 15             if (buffer == null)
 16             {
 17                 this.stream = new MemoryStream();
 18             }
 19             else
 20             {
 21                 this.stream = new MemoryStream(buffer);
 22             }
 23 
 24             this.reader = new BinaryReader(this.stream);
 25             this.writer = new BinaryWriter(this.stream);
 26         }
 27 
 28         public void Close()
 29         {
 30             this.stream.Close();
 31             this.reader.Close();
 32             this.writer.Close();
 33         }
 34 
 35 //-------------------------------------------------------------------------------
 36 
 37         public long ReadInt64()
 38         {
 39             return IPAddress.HostToNetworkOrder(this.reader.ReadInt64());
 40         }
 41 
 42         public int ReadInt32() 
 43         {
 44             return IPAddress.HostToNetworkOrder(this.reader.ReadInt32());
 45         }
 46 
 47         public short ReadInt16() 
 48         {
 49             return IPAddress.HostToNetworkOrder(this.reader.ReadInt16());
 50         }
 51 
 52         public byte ReadByte()
 53         {
 54             return this.reader.ReadByte();
 55         }
 56 
 57         public float ReadFloat()
 58         {
 59             return this.reader.ReadSingle();
 60         }
 61 
 62         public string ReadString8() 
 63         {
 64             return System.Text.Encoding.UTF8.GetString(this.reader.ReadBytes(ReadByte()));
 65         }
 66 
 67         public string ReadString16() 
 68         {
 69             return System.Text.Encoding.UTF8.GetString(this.reader.ReadBytes(ReadInt16()));
 70         }
 71 
 72         public long Seek(long offset)
 73         {
 74             return this.stream.Seek(offset, SeekOrigin.Begin);
 75         }
 76 
 77 //-------------------------------------------------------------------------------
 78 
 79         public void WriteByte(byte value)
 80         {
 81             this.writer.Write(value);
 82         } 
 83 
 84         public void WriteInt16(short value)
 85         {
 86             this.writer.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value)));
 87         }
 88 
 89         public void WriteInt32(int value)
 90         {
 91             this.writer.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value)));
 92         }
 93 
 94         public void WriteInt64(long value)
 95         {
 96             this.writer.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value)));
 97         }
 98 
 99         public void WriteFloat(float value)
100         {
101             this.writer.Write(value);
102         }
103 
104         public void WriteString8(string value)
105         {
106             byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(value);
107 
108             WriteByte((byte) byteArray.Length);
109 
110             this.writer.Write(byteArray);
111         }
112 
113         public void WriteString16(string value)
114         {
115             byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(value);
116 
117             WriteInt16((short) byteArray.Length);
118 
119             this.writer.Write(byteArray);
120         }
121 
122         public byte[] GetBuffer()
123         {
124             return this.stream.ToArray();
125         }
126 
127         public int GetLength()
128         {
129             return (int) this.stream.Length;
130         }
131     }
132 }

 

轉載鏈接:http://blog.csdn.net/qq_26054303/article/details/53019064

       http://blog.csdn.net/tom_221x/article/details/72330107


免責聲明!

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



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