使用FileStream读写文件[通俗易懂]

        static void Mainstring[] args)
        {

            //创建文件
            using FileStream fsWrite = new FileStream@”C:\Users\pqx\Desktop\new.txt”, FileMode.OpenOrCreate, FileAccess.Write))
            {

                string str = “这是要写进文本的内容:行百里者半九十”;
                //将上述字符串转换成字节数组
                byte[] buffer = Encoding.Default.GetBytesstr);
                //写入文本
                fsWrite.Writebuffer, 0, buffer.Length);
                Console.WriteLine“写入成功”);
            }

            //读取文本内容

            using FileStream fsRead = new FileStream@”C:\Users\pqx\Desktop\new.txt”, FileMode.OpenOrCreate, FileAccess.Read))
            {

                //定义一个字节缓冲区,大小为2M
                byte[] buffer1 = new byte[1024 * 1024 * 2];
                int r = fsRead.Readbuffer1, 0, buffer1.Length);
                //将字节数组中的每一个元素按照指定的编码格式解码成字符串
                string s = Encoding.Default.GetStringbuffer1, 0, r);
                Console.WriteLines);
            }
         Console.ReadKey);
        }

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注