Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用;使用;循环中包含StreamWriter/Reader的语句_C#_Using_Streamreader_Streamwriter - Fatal编程技术网

C# 使用;使用;循环中包含StreamWriter/Reader的语句

C# 使用;使用;循环中包含StreamWriter/Reader的语句,c#,using,streamreader,streamwriter,C#,Using,Streamreader,Streamwriter,这是我的情况 从文本文件中读取一行 “加工”生产线 将“已处理”行写入新文本文件 循环到#1并重复,直到EOF 我是这样做的: using (StreamReader srReader = new StreamReader(strInputFile)) { // loop until EOF while ((strCurrentLine = srReader.ReadLine()) != null) { // "process" strCurrentLin

这是我的情况

  • 从文本文件中读取一行
  • “加工”生产线
  • 将“已处理”行写入新文本文件
  • 循环到#1并重复,直到EOF
  • 我是这样做的:

    using (StreamReader srReader = new StreamReader(strInputFile))
    {
        // loop until EOF
        while ((strCurrentLine = srReader.ReadLine()) != null)
        {
            // "process" strCurrentLine...
    
            // write the "processed" strCurrentLine to a new text file
            using (StreamWriter sw = new StreamWriter(strFullOutputPathFileName, false))
            {
                // write strCurrentLine to the new file
                sw.WriteLine("stuff...");
            }
         }
    }
    
    我的经理告诉我,像在循环中一样使用
    using
    语句将极大地阻碍性能。原因是
    StreamWriter
    实例的创建次数与我循环的次数相同。因此,如果我循环1000次,我的
    StreamWriter
    对象将有1000个实例,这可能会严重影响性能


    这是真的吗?另外,我上面的方法是实现这一点的最佳方法吗?

    您应该尽可能少地打开和关闭文件。 因此,在while循环之前打开它(创建streamwriter)。 在循环中使用sw.WriteLine(“stuff…”) 在循环调用sw.Close()之后

    从某种意义上讲,您的经理是错误的,因为它不会创建1000个实例,因为每个实例都将在迭代结束时发布,但您将打开和关闭文件,这将影响性能

    问候,, 达姆扬

    我的经理告诉我,像在循环中一样使用using语句将极大地降低性能。原因是StreamWriter实例的创建次数与我循环的次数相同。因此,如果我循环1000次,我的StreamWriter对象将有1000个实例,这可能会严重影响性能

    这是真的吗

    这是真的,但不是因为您正在创建实例,而是因为您正在打开和关闭一个文件1000次。您可以创建1000个字符串,对性能几乎没有影响

    另外,我的上述方法是实现这一目标的最佳方法吗

    要开始,请将编写器创建移动到
    while
    循环之外:

    using (StreamReader srReader = new StreamReader(strInputFile))
    {
        // write the "processed" strCurrentLine to a new text file
        using (StreamWriter sw = new StreamWriter(strFullOutputPathFileName, false))
        {
            // loop until EOF
            while ((strCurrentLine = srReader.ReadLine()) != null)
            {
                // "process" strCurrentLine...
    
                // write strCurrentLine to the new file
                sw.WriteLine("stuff...");
            }
         }
    }
    
    但是,您也可以将整个文件读入内存,对其进行处理,然后在一次操作中将其写出。影响将取决于所做的处理,以及如果出现错误,是否需要部分结果。

    将代码更改为:

    using (StreamReader srReader = new StreamReader(strInputFile))
    {
        using (StreamWriter sw = new StreamWriter(strFullOutputPathFileName, false))
        {
            while ((strCurrentLine = srReader.ReadLine()) != null)
            {
                sw.WriteLine("stuff...");
            }
        }
    }
    

    另外,请查看Jon关于追加的评论。

    是的,通过这种方式,您将创建许多StreamWriter(但不会有许多实例同时运行)。解决这一问题的一个简单方法是在您进入while循环之前创建StreamWriter。

    如果您的文件不大,您可以使用file.ReadAllLines获取字符串数组中的文件行,然后再进行处理。

    当然,比性能更大的问题是您没有附加文件,所以您的最终文件将是输入文件的最后一行…
    我会有1000个实例
    不。但是您会创建1000次。using语句不是性能问题,但是调用“new StreamWriter”1000次而不是一次是有问题的。不过,正如其他评论所指出的,您正在将文件覆盖999次这一事实是一个更大的问题。请参阅。我更喜欢使用
    File.ReadAllText()
    File.writealText()
    而不是
    StreamReader
    StreamWriter
    @JonSkeet是的,我在追加…我复制了错误的行否,它将创建1000个实例。它们不会同时都是“活动”的,但它肯定会创建1000个实例……而且声称每个实例将在迭代开始时发布也是不准确的;它们可能会被释放,但不能保证垃圾收集器何时处理它们。它可能会很快发生,但除非我弄错了,否则这些实例在理论上可能会在超出范围后在内存中继续存在一段时间。我同意你们乔恩和克雅尔坦的观点。删除外部
    {}是惯例
    并将usings语句放在同一缩进级别。我见过它在使用,但不知道它是惯例。谢谢你的信息。