C# 中的Stream流

时间:2022-06-01 17:46:14

流就是一个类的对象,很多文件的输入输出操作都以类的成员函数的方式来提供;

流其实是一种信息的转换,是有序的,有输入和输出流(IO);

1.FileStream

文件流,读取和保存文件操作使用;

//写入
FileStream fs = new FileStream("data.txt", FileMode.OpenOrCreate);
string msg = "littlePerilla";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(msg);
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
fs.Close(); //读取
FileStream fs = new FileStream("data.txt", FileMode.Open, FileAccess.Read);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
char[] c = Encoding.UTF8.GetChars(bytes);
fs.Flush();
fs.Close();

2.StreamReader 和 StreamWriter

粒度为字符的流;

void StreamWrite()
{
string path = "test.txt";
//创建StreamWriter 类的实例
StreamWriter streamWriter = new StreamWriter(path);
streamWriter.WriteLine("Perilla");
streamWriter.WriteLine("13112345678");
//刷新缓存
streamWriter.Flush();
//关闭流
streamWriter.Close();
} void StreamReader()
{
//定义文件路径
string path = "test.txt";
//创建 StreamReader 类的实例
StreamReader streamReader = new StreamReader(path);
//判断文件中是否有字符
while (streamReader.Peek() != -1)
{
//读取文件中的一行字符
string str = streamReader.ReadLine();
Debug.Log(str);
}
streamReader.Close();
}

3.BinaryWriter 和 BinaryReader

粒度为字节的读写流;

// 读取文件
void ReadFile()
{
FileStream fs = new FileStream("data.txt", FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs); //以二进制方式读取文件中的内容
int i = r.ReadInt32();
float f = r.ReadSingle();
double d = r.ReadDouble();
bool b = r.ReadBoolean();
string s = r.ReadString();
Debug.Log(i);
Debug.Log(f);
Debug.Log(d);
Debug.Log(b);
Debug.Log(s); r.Close();
fs.Close();
} // 写入文件
void WriteFile()
{
FileStream fs = new FileStream("data.txt", FileMode.OpenOrCreate);
BinaryWriter w = new BinaryWriter(fs); //以二进制方式向创建的文件中写入内容
w.Write(1123);
w.Write(0.3135f);
w.Write(0.3188946);
w.Write(true);
w.Write("LittlePerilla"); w.Close();
fs.Close();
}

4.MemoryStream

内存的读写流,字节粒度,支持Position和Seek操作,*度更高;

支持异步读写,不需要手动释放和开辟内存;

支持在任意位置修改操作;

class Program
{
static void Main(string[] args)
{
string strValue = "LittlePerillaIsSuperHero";
MemoryStream ms = new MemoryStream();
ms.Write(Encoding.UTF8.GetBytes(strValue), 0, strValue.Length); Console.WriteLine(ms.Position);
//打印测试
byte[] byte1 = ms.GetBuffer();
string str1 = Encoding.UTF8.GetString(byte1);
Console.WriteLine(str1); ms.Seek(2, SeekOrigin.Current);
ms.ReadByte();
ms.ReadByte();
ms.ReadByte();
ms.ReadByte();
byte[] bytes3 = ms.ToArray();
foreach (byte b in bytes3)
{
Console.Write(b + "-");
}
str1 = Encoding.UTF8.GetString(bytes3);
Console.WriteLine("\n"+str1);
//这里说明ms.ReadByte不会截断读完的数据 MemoryStream ms2 = new MemoryStream();
byte[] bytes6 = Encoding.UTF8.GetBytes("abcde");
ms2.Write(bytes6, 0, bytes6.Length);
Console.WriteLine(ms2.Position); //等价
ms2.Position = 0;//ms2.Seek(0, SeekOrigin.Begin); byte[] byteArray = new byte[5] { 110, 110, 110, 110, 110 };
ms2.Read(byteArray, 2, 1);
Console.WriteLine(Encoding.UTF8.GetString(byteArray));
//结果为nnann,说明讲ms2中的数据读进byteArray中偏移2的位置,且只读取1个字节; //指定位置写入
MemoryStream ms3 = new MemoryStream();
byte[] bytesArr = Encoding.ASCII.GetBytes("abcdefg");
ms3.Write(bytesArr, 0, bytesArr.Length);
ms3.Position = 2;
ms3.WriteByte(97); //97代表的是a 这段代码的意思是,将原先第二个的c替换为a
string str = Encoding.ASCII.GetString(ms3.ToArray());
Console.WriteLine(str); //输出 abacdefg Console.ReadKey();
} }

5.NetworkStream

为网络访问提供数据的基础流;用于 Stream 在阻止模式下通过套接字发送和接收数据的方法;

可以将类用于 NetworkStream 同步和异步数据传输;

创建NetworkStream必须提供Socket

详细不适合在这里讨论;待完善吧;