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”的确是互相关联,释放其中一个,会对另外一个造成一定的影响,

  • 相关阅读:
    iOS开发之静态库(二)—— .a
    iOS开发之静态库(一)—— 基本概念
    Linux中ctrl-c, ctrl-z, ctrl-d 区别
    JNI技术基础(1)——从零开始编写JNI代码
    开篇纪念
    java面试题
    jvm系列二之GC收集器
    jvm系列一
    ConcurrentHashMap源码剖析(1.8版本)
    博客系统对比
  • 原文地址:https://www.cnblogs.com/hhhh2010/p/5210306.html
Copyright © 2011-2022 走看看