VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C#教程之C#的字节与流

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

  计算机中文件有很多种,我们知道实际存在计算机中的都是二进制。这里我记录了通过流对文件的读取操作。

     一、首先在这里简单涉及下位,字节,字符的概念。

     位(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

相关教程