关闭文件,而无需使用使用 [英] Closing a file without using using

查看:122
本文介绍了关闭文件,而无需使用使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类从一个文件流中读取数据,并写入到另一个。我关心闭流的处理closeFiles()结束后。



您会如何处理一个流的处置可能会引发异常停止的可能性被称为其他流的处理。



我应该调用close和处置的流或只是一个?



如果我从流处置捕捉任何错误,然后如图lastOperation移动和文件的删除继续会发生什么()?



在一个完美的世界,我想在C ++风格的初始化列表中使用using语句,但我敢肯定,在C#中这是不可能的。



修改:谢谢你的快速反应的家伙。那么,我应该做的是自IDisposable派生,然后改变其构造函数,并添加像这样两种处理方式:

 〜 FileProcessor()
{
的Dispose(真);
}

公共无效的Dispose()
{
的Dispose(真);
GC.SuppressFinalize(本);
}
私人无效的Dispose(BOOL处置)
{
如果(!this.disposed)
{
如果(处置)
{
sw.Flush();
}
closeFiles();
处置= TRUE;
}
}

这基本上是我在做什么:

 类FileProcessor 
{
私人字符串in_filename;
私人字符串out_filename;
私人StreamReader的SR;
私人StreamWriter的SW;
布尔filesOpen = FALSE;

公共FileProcessor(字符串文件名)
{
in_filename =文件名;
out_filename =文件名+.OUT;
openFiles散();
}

〜FileProcessor()
{
closeFiles();
}

私人无效openFiles散()
{
SR =新的StreamReader(in_filename);
SW =新的StreamWriter(out_filename);
filesOpen = TRUE;
}

私人无效closeFiles()
{
如果(filesOpen)
{
sr.Close();
sw.Close();
sr.Dispose();
sw.Dispose();
filesOpen = FALSE;
}
}

/ *各种功能来读取,处理和写入文件* /

公共无效lastOperation()
{
closeFiles();
File.Delete(in_filename);
Directory.Move(out_filename,OUTDIR + out_filename);
}
}


解决方案

您FileProcessor类不应该有一个析构函数。这是没有用的,但它是昂贵的。



它应该有一个的Dispose()(和实现IDisposable接口)调用closeFiles()。



而像@marcelo回答,Stream.Dispose()应该不会抛出。你可以靠这个了BCL类。



但是,你应该检查每个读/写卡为空,如果第一个打开,但第二个失败:

 如果(!SR = NULL)sr.Dispose(); 
如果(!SW = NULL)sw.Dispose();

filesOpen 不能涵盖。


I have a class which reads data from one file stream and writes to another. I am concerned about closing the streams after the processing has finished in closeFiles().

How would you handle the possibility that the dispose of one stream may throw an exception stopping the dispose of the other stream from being called.?

Should I be calling close and dispose on the streams or just one?

What happens if I catch any errors from the stream disposes and then continue with moving and deleting of the files as shown in lastOperation()?

In a perfect world I'd like to use a using statement in a c++ style initialisation list but I'm pretty sure that's not possible in c#.

EDIT : thanks for the quick responses guys. So what I should be doing is deriving from IDisposable and then change the constructor and add the two disposing methods like this?:

    ~FileProcessor()
    {
        Dispose(true);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    private void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                sw.Flush();
            }
            closeFiles();
            disposed = true;
        }
    }

This is basically what I'm doing:

class FileProcessor
{
    private string in_filename;
    private string out_filename;
    private StreamReader sr;
    private StreamWriter sw;
    bool filesOpen = false;

    public FileProcessor(string filename)
    {
        in_filename = filename; 
        out_filename = filename + ".out";
        openFiles();
    }

    ~FileProcessor()
    {
        closeFiles();
    }

    private void openFiles()
    {
        sr = new StreamReader(in_filename);
        sw = new StreamWriter(out_filename);
        filesOpen = true;
    }

    private void closeFiles()
    {
        if (filesOpen)
        {
            sr.Close();
            sw.Close();
            sr.Dispose();
            sw.Dispose();
            filesOpen = false;
        }
    }

    /* various functions to read, process and write to the files */

    public void lastOperation()
    {
        closeFiles();
        File.Delete( in_filename );
        Directory.Move(out_filename, outdir + out_filename);
    }
}

解决方案

Your FileProcessor class should not have a destructor. It is of no use but it is expensive.

It should have a Dispose() (and implement the IDisposable interface) to call closeFiles().

And like @marcelo answered, Stream.Dispose() should not throw. You can rely on this for BCL classes.

But you should check each Reader/Writer for null, in case the first one opened but the second one failed:

if (sr != null) sr.Dispose();
if (sw != null) sw.Dispose();

Your filesOpen can't cover both.

这篇关于关闭文件,而无需使用使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆