关闭StreamWriter和StreamReader的最佳和正确方法

时间:2017-07-17 18:21:58

标签: c#

我一直在尝试组织一个混乱的代码!此时第一个也是我最大的问题是我的一个StreamWriters或StreamReader正在打开。使用this link,我正在尝试组织我的代码。但我的问题是我不知道应该在哪里关闭它:

我的代码是:

public static void ProcessFile(string[] ProcessFile, int id_customer, string directoryinprocess)
{
    StreamWriter Writer = null, Writer2 = null, Writer3 = null;

    foreach (string filename in ProcessFile)
    {

        // Used for the output name of the file
        var dir = Path.GetDirectoryName(filename);
        var fileName = Path.GetFileNameWithoutExtension(filename);
        var ext = Path.GetExtension(filename);
        var folderbefore = Path.GetFullPath(Path.Combine(dir, @"..\"));
        int rowCount = 0;
        string path_body_out = "";
        string outputname = folderbefore + "output_temp\\" + fileName;

        if (filename.Contains("RO_"))
        {
            Writer = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_hd_intermediate" + ext) { AutoFlush = true };
            Writer2 = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_body_out" + ext) { AutoFlush = true };
            path_body_out = dir + "\\" + "output_temp\\" + fileName + "_hd_intermediate" + ext;
        } // end of if
        else
        {
            Writer3 = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_out" + ext) { AutoFlush = true };
        } // end of else

        using (StreamReader Reader = new StreamReader(@filename))
        {
            while (!Reader.EndOfStream)
            {
                string inputLine = string.Empty;
                inputLine = Reader.ReadLine();

                rowCount++;

                if (filename.Contains("RO_"))
                {
                    if (rowCount <= 4)
                    {
                            Writer.WriteLine(inputLine);
                    }
                    if (rowCount >= 5)
                    {
                        Writer2.WriteLine(inputLine);
                    }
                }
                else
                {
                    { Writer3.WriteLine(inputLine); }
                }

            } // end of the while
        } // end of using Stremreader


        if (path_body_out.Contains("_hd_intermediate"))
        {
            ManipulateHeaderFilesTypeRo(dir, path_body_out);
        }
        else
        { }
    } // end of the foreach


    string[] extensions = { "_fv", "_body", "_out" };

    string[] fileEntriesout = System.IO.Directory.EnumerateFiles(directoryinprocess, "*.csv", System.IO.SearchOption.AllDirectories)
    .Where(file => extensions.Any(ex => Path.GetFileNameWithoutExtension(file).EndsWith(ex)))
        .ToArray();


    foreach (string filenameout in fileEntriesout)
    {
        string destinytablename = null;

        if (filenameout.Contains("_hd_intermediate_fv"))
        { destinytablename = "TBL_DATA_TYPE_RO_HEADER"; }
        else if (filenameout.Contains("_body_out"))
        { destinytablename = "TBL_DATA_TYPE_RO_BODY"; }
        else
        { destinytablename = "TBL_DATA_TYPE_LOAD"; }

        string id_file = Get_id_file(filenameout, id_customer);

        DataTable csvFileData = GetDataTabletFromCSVFile(filenameout, id_file);

        InsertDataIntoSQLServerUsingSQLBulkCopy(csvFileData, destinytablename);

    } // end of the foreach

    //} // end of the foreach

} // end of ProcessFile 
  • 问题: 我应该如何关闭该部分:

        if (filename.Contains("RO_"))
        {
            Writer = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_hd_intermediate" + ext) { AutoFlush = true };
            Writer2 = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_body_out" + ext) { AutoFlush = true };
            path_body_out = dir + "\\" + "output_temp\\" + fileName + "_hd_intermediate" + ext;
        } // end of if
        else
        {
            Writer3 = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_out" + ext) { AutoFlush = true };
        } // end of else
    
        using (StreamReader Reader = new StreamReader(@filename))
        {
            while (!Reader.EndOfStream)
            {
                string inputLine = string.Empty;
                inputLine = Reader.ReadLine();
    
                rowCount++;
    
                if (filename.Contains("RO_"))
                {
                    if (rowCount <= 4)
                    {
                            Writer.WriteLine(inputLine);
                    }
                    if (rowCount >= 5)
                    {
                        Writer2.WriteLine(inputLine);
                    }
                }
                else
                {
                    { Writer3.WriteLine(inputLine); }
    

我应该在这里关闭吗?

        if (filename.Contains("RO_"))
        {
            Writer = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_hd_intermediate" + ext) { AutoFlush = true };
            Writer2 = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_body_out" + ext) { AutoFlush = true };
            path_body_out = dir + "\\" + "output_temp\\" + fileName + "_hd_intermediate" + ext;
        } // end of if
        else
        {
            Writer3 = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_out" + ext) { AutoFlush = true };
        } // end of else

或者在这里?

                if (filename.Contains("RO_"))
                {
                    if (rowCount <= 4)
                    {
                            Writer.WriteLine(inputLine);
                    }
                    if (rowCount >= 5)
                    {
                        Writer2.WriteLine(inputLine);
                    }
                }
                else
                {
                    { Writer3.WriteLine(inputLine); }
                }

2 个答案:

答案 0 :(得分:5)

如果您无法重新组织此代码,以便每个StreamWriter实例都可以包含在using()中,那么您可以执行以下操作:

StreamWriter Writer = null, Writer2 = null, Writer3 = null;

try
{
    // your existing code
}
catch
{
    // Handle
}
finally
{
    if (Writer != null)
        Writer.Close();
    if (Writer2 != null)
        Writer2.Close();
    if (Writer3 != null)
        Writer3.Close();
}

这可确保无论try内发生什么错误,您的作家都将被关闭。

在我看来,有条件地实例化对象是一种气味,你应该根据filename.Contains("RO_")进行不同的实现。您可以使用策略模式并具有不同的文件处理器接口实现,根据文件名选择正确的文件处理器接口实现。每个实现只知道如何写入它需要的位置。这将允许您在每个作者周围正确使用using()

答案 1 :(得分:1)

如果您使用的是一次性物品,我会说使用using块。但是,由于你有条件地设置一次性物品,我认为使用try-finally块是你最好的选择。

声明一次性对象,并在try块之外将它们初始化为null。

将一次性对象初始化为try块内所需的实例。创建一次性对象后,请注意不要在try-block内部的任何位置更改此引用。

同样在你的try块内,做你需要做的一次性物品。

在try块之后创建一个finally块(一个catch块是可选的,但是你需要一个finally块来让这个方法完成它的工作。)并在finally块中,检查你声明的变量是否包含一次性块对象不是空的。如果它们不为null,则关闭它们并使它们为空。

StreamWriter writer = null;

try {
    if (condA) {
       writer = new StreamWriter("filePath1");
    } else if (condB) {
        writer = new StreamWriter("filePath2");
    } else {
        writer = new StreamWriter("filePath3");
    }

    // do things with writer

} catch (Exception ex) {

} finally {
    if (writer != null) {
        writer.close();
        writer = null;
    }
}