zoukankan      html  css  js  c++  java
  • (整理)streamWriter、streamReader与FileStream

          今天偶然使用VS代码分析,发现CA2000警告,然后其中一条为streamWriter、streamReader与FileStream相关内容,特查询并记录一下。

          引文地址:http://bbs.csdn.net/topics/390313584;

            http://blog.csdn.net/jaychouliyu/article/details/6152256

            实现代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Threading;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
                //StreamReader sr = new StreamReader(fs);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write("Hi there");
                //注意:调用StreamWriter.Close会同时关闭其使用的FileStream对象,而FileStream对象不需要显示地关闭  
                //sw.Close();
                sw.Dispose();
                fs.Dispose();
                
                Thread.Sleep(4000);
                if(sw == null)
                {
                    Console.WriteLine("Null");
                }
                else
                {
                    Console.WriteLine("not Null");
                }
                Console.ReadLine();
            }
        }
    }

            先释放“sw”后释放“fs”时,确实会出现“对象未按所有异常路径释放”;如果先释放“fs”,后释放“sw”,出现“ObjectDisposeException”。“fs”与“sw”的确是互相关联,释放其中一个,会对另外一个造成一定的影响,

  • 相关阅读:
    前端安全【面试】
    防xss攻击
    前端工程化
    前端项目构建——运维
    react入门
    OpenGL Windows 窗口程序环境搭建
    Django 列的自定义显示
    设计模式之 SOA面向服务的体系
    设计模式之Builder建造者模式 代码初见
    设计模式之Factory工厂模式的好处
  • 原文地址:https://www.cnblogs.com/hhhh2010/p/5210306.html
Copyright © 2011-2022 走看看