C#|C#文件流FileStream

class Program { static void Main(string[] args) { // FileStream // 写 #region Writebyte //using (FileStream fWriter = new FileStream("txt.txt", FileMode.Create, FileAccess.Write)) //{ //// //// fWriter.WriteByte(97); //string s = Console.ReadLine(); //// byte[] bs = Encoding.Default.GetBytes(s); //// ff fe //fWriter.WriteByte(255); //fWriter.WriteByte(254); //byte[] bs = Encoding.Unicode.GetBytes(s); //for (int i = 0; i < bs.Length; i++) //{ //fWriter.WriteByte(bs[i]); //}//} #endregion#region Write //using (FileStream fWriter = new FileStream("txt.txt", FileMode.Create, FileAccess.Write)) //{ ////abc019AB //byte[] bs = { 97, 98, 99, 48, 49, 57, 65, 66 }; //fWriter.Write(bs, 1, bs.Length - 1); //} #endregion// 读 #region ReadByte //using (FileStream fReader = new FileStream("txt.txt", FileMode.Open, FileAccess.Read)) //{ //// bc019AB ////int i1 = fReader.ReadByte(); ////int i2 = fReader.ReadByte(); ////int i3 = fReader.ReadByte(); ////int i4 = fReader.ReadByte(); ////int i5 = fReader.ReadByte(); ////int i6 = fReader.ReadByte(); ////int i7 = fReader.ReadByte(); ////int i8 = fReader.ReadByte(); //List list = new List(); //int res = -1; //while ((res = fReader.ReadByte()) != -1) //{ //list.Add((byte)res); //} //} #endregion#region Read //using (FileStream fReader = new FileStream("txt.txt", FileMode.Open, FileAccess.Read)) //{ //// 用数组来存储一个块字节 //// byte[] bs = new byte[1024 * 1024]; // 迅雷是不是有一个缓存//byte[] bs = new byte[10]; //int count = fReader.Read(bs, 1, bs.Length-1); // 要求读取10个字节,从数组bs的第0个位置开始存储//} #endregion// 使用FileStream读取文本文件,注意里面有汉字// 复制 Console.Write("请输入要复制的文件#"); string fileSource = Console.ReadLine(); if (!File.Exists(fileSource)) { Console.WriteLine("文件不存在,请按任意键结束"); Console.ReadKey(); return; }// 存在 Console.Write("请输入保存的路径名#"); string fileSave = Console.ReadLine(); using (FileStream fileReader = new FileStream(fileSource, FileMode.Open, FileAccess.Read)) { using (FileStream fileWriter = new FileStream(fileSave, FileMode.Create, FileAccess.Write)) { // 字节处理,作死 //int res = -1; //while ((res = fileReader.ReadByte()) != -1) //{ //fileWriter.WriteByte((byte)res); //}// 字节数组 byte[] bs = new byte[1024 * 1024 * 10]; int count = 0; while ((count = fileReader.Read(bs, 0, bs.Length)) > 0) { fileWriter.Write(bs, 0, count); }} }Console.WriteLine("OK"); Console.ReadKey(); } }

【C#|C#文件流FileStream】

    推荐阅读