首页 > 其他 > 详细

File FileStream StreamReader和StreamWriter

时间:2020-02-15 16:41:33      阅读:65      评论:0      收藏:0      [点我收藏+]

File 静态类 ReadAllBytes 和 WriteAllBytes ,用于一次性全部读取和写入小文件的字节码,
                  ReadLine  ReadkAll  用于一次性全部读取字符(字符串)
FileStream 采用流的方式,适用于读取任意大小的文件的字节码,

static void CopyAvi(string source, string target)
        {
            using (FileStream fsReader = new FileStream(source, FileMode.OpenOrCreate, FileAccess.Read))
            {
                using (FileStream fsWriter = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    byte[] buffer = new byte[1024 * 1024 * 5];
                    while (true)
                    {
                        int length=  fsReader.Read(buffer, 0, buffer.Length);
                        if (length <= 0)
                        {
                            break;
                        }
                        fsWriter.Write(buffer, 0, length);
                    }
                }
            }
        }

 

StreamReader和StreamWriter 采用流的方式,用于任意大小文件的字符操作

    static void StreamReaderMethod(string path)
        {
            using (StreamReader sr = new StreamReader(path, Encoding.UTF8)) {
                while (!sr.EndOfStream) {
                    Console.WriteLine( sr.ReadLine());
                }
            }
        }

        static void StreamWriterMethod(string path) {
            using (StreamWriter sw = new StreamWriter(path))
            {
                sw.WriteLine("看看有没有覆盖");
            }
        }

 

File FileStream StreamReader和StreamWriter

原文:https://www.cnblogs.com/boentouch/p/12312173.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!