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等。流的内部就是对二进制数据的读取。
流可以一次性读取,也可以循环读取。
一次性读取:
public void Read()
{
string filePath = Environment.CurrentDirectory + "/content.txt";
Stream source = new FileStream(filePath, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[source.Length];
int bytesRead = source.Read(buffer, , (int)source.Length); Output(buffer);
}
循环读取:
public void ReadLoop()
{
string filePath = Environment.CurrentDirectory + "/content.txt";
string fileDest = Environment.CurrentDirectory + "/dest.txt";
Stream source = new FileStream(filePath, FileMode.Open, FileAccess.Read);
Stream destStream = new FileStream(fileDest, FileMode.Create, FileAccess.Write); int bufferSize = ;
byte[] buffer = new byte[bufferSize];
int bytesREad;
while((bytesREad= source.Read(buffer, , bufferSize)>))
{
destStream.Write(buffer, , bytesREad);
}
source.Dispose();
destStream.Dispose();
}
StreamReader, StreamWriter。StringReader, StringWriter。它们是流的包装器类,方便对流的读取。以下是示例代码:
public void StreamReaderRead()
{
string filePath = Environment.CurrentDirectory + "/content.txt";
Stream source = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(source);//Stream包装器类
string text = sr.ReadToEnd(); Console.Write(text);
}
public void StreamWriterWrite()
{
string filePath = Environment.CurrentDirectory + "/content.txt";
string fileDest = Environment.CurrentDirectory + "/dest1.txt"; Stream source = new FileStream(filePath, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(source);//Stream包装器类
string text = sr.ReadToEnd(); StreamWriter sw = new StreamWriter(fileDest);//Stream包装器类
sw.Write(text);
sw.Flush();
sw.Close();
}
三、二进制字节流读写封装
完成以下功能:
- 只针对内存字节流的读写,主要应用于数据的解析和写入。
- 提供不同数据类型的读写接口,包括byte,short,int,float,string等。
- 处理了大小端数据转换的问题,所以可用于网络数据的解析和发送。
using System.IO;
using System.Net;
using System; namespace Framework
{
public class NetStream
{
private MemoryStream stream;
private BinaryReader reader;
private BinaryWriter writer; public NetStream(byte[] buffer = null)
{
if (buffer == null)
{
this.stream = new MemoryStream();
}
else
{
this.stream = new MemoryStream(buffer);
} this.reader = new BinaryReader(this.stream);
this.writer = new BinaryWriter(this.stream);
} public void Close()
{
this.stream.Close();
this.reader.Close();
this.writer.Close();
} //------------------------------------------------------------------------------- public long ReadInt64()
{
return IPAddress.HostToNetworkOrder(this.reader.ReadInt64());
} public int ReadInt32()
{
return IPAddress.HostToNetworkOrder(this.reader.ReadInt32());
} public short ReadInt16()
{
return IPAddress.HostToNetworkOrder(this.reader.ReadInt16());
} public byte ReadByte()
{
return this.reader.ReadByte();
} public float ReadFloat()
{
return this.reader.ReadSingle();
} public string ReadString8()
{
return System.Text.Encoding.UTF8.GetString(this.reader.ReadBytes(ReadByte()));
} public string ReadString16()
{
return System.Text.Encoding.UTF8.GetString(this.reader.ReadBytes(ReadInt16()));
} public long Seek(long offset)
{
return this.stream.Seek(offset, SeekOrigin.Begin);
} //------------------------------------------------------------------------------- public void WriteByte(byte value)
{
this.writer.Write(value);
} public void WriteInt16(short value)
{
this.writer.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value)));
} public void WriteInt32(int value)
{
this.writer.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value)));
} public void WriteInt64(long value)
{
this.writer.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value)));
} public void WriteFloat(float value)
{
this.writer.Write(value);
} public void WriteString8(string value)
{
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(value); WriteByte((byte) byteArray.Length); this.writer.Write(byteArray);
} public void WriteString16(string value)
{
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(value); WriteInt16((short) byteArray.Length); this.writer.Write(byteArray);
} public byte[] GetBuffer()
{
return this.stream.ToArray();
} public int GetLength()
{
return (int) this.stream.Length;
}
}
}
转载链接:http://blog.csdn.net/qq_26054303/article/details/53019064
http://blog.csdn.net/tom_221x/article/details/72330107
C#的字节与流的更多相关文章
- Java:IO流其他类(字节数组流、字符数组流、数据流、打印流、Properities、对象流、管道流、随机访问、序列流、字符串读写流)
一.字节数组流: 类 ByteArrayInputStream:在构造函数的时候,需要接受数据源,而且数据源是一个字节数组. 包含一个内部缓冲区,该缓冲区包含从流中读取的字节.内部计数器跟踪 read ...
- Java基础知识强化之IO流笔记28:BufferedOutputStream / BufferedInputStream(字节缓冲区流) 之BufferedOutputStream写出数据
1. BufferedOutputStream / BufferedInputStream(字节缓冲区流)的概述 通过定义数组的方式确实比以前一次读取一个字节的方式快很多,所以,看来有一个缓冲区还是非 ...
- Java IO学习笔记(三)转换流、数据流、字节数组流
转换流 1.转换流:将字节流转换成字符流,转换之后就可以一个字符一个字符的往程序写内容了,并且可以调用字符节点流的write(String s)方法,还可以在外面套用BufferedReader()和 ...
- JAVA IO分析二:字节数组流、基本数据&对象类型的数据流、打印流
上一节,我们分析了常见的节点流(FileInputStream/FileOutputStream FileReader/FileWrite)和常见的处理流(BufferedInputStream/B ...
- IO知识点整理(序列化,管道流,数据流,字节数组流,与编码)
一:序列化的问题 1.序列号的使用问题 关于在序列化中的序列号的使用问题,一般要是使用. 因为,每次要序列化的类产生都会产生一个一个新的序列号,如果将这个类的程序修改后,就会产生新的序列号,以前序列化 ...
- Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream)
Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在我们学习字 ...
- 6.5(java学习笔记)其他流(字节数组流,数据流,对象流,打印流)
一.字节数组流 之前使用输入输出流的操作的对象是文件,而这里字节数组流操作的对象是内存,内存可以看做是一个字节数组. 使用字节数组流读写就可以看做是从内存A到内存B的读写,对象时内存即字节数组. 1. ...
- java——io、字节流缓冲区拷贝文件、字节缓冲流
使用try catch finally关闭文件流: 写入文件: import java.io.*; public class exp{ public static void main(String[] ...
- Java文件与io——字节数组流数据流字符串流
字节数组流 ByteArrayInputStream:包含一个内部缓冲区,该缓冲区包含从流中读取的字节.内部计数器跟踪read方法要提供的下一个字节.关闭ByteArrayInputStream无效. ...
- Java基础IO类之字节数组流
package IODemo; //字节数组流 :内部维护这着一个字节数组,我们可以利用流的读取机制来处理字符串 无需关闭,不会报IO异常 // ByteArrayInputstream ByteAr ...
随机推荐
- 1002. Find Common Characters
Given an array A of strings made only from lowercase letters, return a list of all characters that s ...
- jzoj5832. 【省选模拟8.20】Emotional Flutter
tj:我們發現,每一次走過的步長都是k,設當前走的步數是x,走到了一個白條 那麼,每一次走就是把所有黑條都向前移k位,我們可以考慮把所有黑條的左邊界不斷的向前移動k,直到下一次移動時,其左邊界小於0, ...
- 一:使用maven构建项目
一般情况下:使用maven构建项目有两种情况: 1:用maven构建java项目: 2:用maven构建javaweb项目: 还有一种经常需要使用到的就是用maven构建项目模块:如:一个父项目用来作 ...
- 对Spring 及SpringMVC的理解
spring是一个轻型容器(light-weight Container),其核心是Bean工厂(Bean Factory),用以构造我们所需要的M(Model).在此基础之上,Spring提供了AO ...
- 初次学习Vue,输出Hello Vue!
Vue.js作为目前比较流行的js框架,而我却迟迟没有接触,深感不安! 使用vue之前先要下载vue.js文件,然后在html里面导入vue.js文件,下面试着输出"Hello Vue!&q ...
- 【9】JMicro微服务-发布订阅消息服务
如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl 1. JMicro消息服务目前实现特性 a. JMicro只支持发布订阅消息服务,不支持队列式消息服务: b. 不支持消息持 ...
- 获取dictionary 值连续相同的索引,
; i < ;) { , i]; var rangType = companyScheme[i]; string txtCell = ""; switch (rangType ...
- [摘]HttpContext, HttpRequest, HttpResponse, HttpRuntime, HttpServerUtility
[摘]http://www.cnblogs.com/fish-li/archive/2011/08/21/2148640.html HttpRuntime HttpRuntime公开了一个静态方法 U ...
- CSS3中的pointer-events
今天做项目中偶然误把元素加上了pointer-events属性,结果导致后来在js中给该元素加点击事件不能用,检查了半天才发现是这个属性的问题.之前没有好好研究,于是决定仔细研究一下. 一.定义及语法 ...
- jetbrains golang IDE
非常好的IDE,叫goland. 支持最新的golang1.8了 下载地址: https://www.jetbrains.com/go/ 开始使用手册: https://www.jetbrains.c ...