【问题标题】:StreamWriter Not working in C#StreamWriter 在 C# 中不工作
【发布时间】:2015-09-02 18:18:48
【问题描述】:

这段代码在 VS 2010 中完美运行。现在我有了 VS 2013,它不再写入文件。它没有错误或任何东西。 (我在 Notepad++ 中收到一条警报,指出文件已更新,但没有写入任何内容。)

在我看来一切都很好。有什么想法吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            String line;
            try
            {
                //Pass the file path and file name to the StreamReader constructor
                StreamReader sr = new StreamReader("C:\\Temp1\\test1.txt");
                StreamWriter sw = new StreamWriter("C:\\Temp2\\test2.txt");

                //Read the first line of text
                line = sr.ReadLine();

                //Continue to read until you reach end of file
                while (line != null)
                {
                    //write the line to console window
                    Console.WriteLine(line);
                    int myVal = 3;
                    for (int i = 0; i < myVal; i++)
                    {
                        Console.WriteLine(line);
                        sw.WriteLine(line);
                    }
                    //Write to the other file
                    sw.WriteLine(line);
                    //Read the next line
                    line = sr.ReadLine();
                }

                //close the file
                sr.Close();
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }
        }
    }
}

【问题讨论】:

  • 您在寻找正确的目录吗?您是否已将文件作为完整性检查打开?
  • 也尝试显式刷新。
  • 我没有看到你调用 sw.Close();如果你这样做,写入将被刷新并关闭文件。此外,您应该真正考虑将 StreamReader 和 StreamWriter 包装在 using 块中 - 它们都实现 IDisposable,并且在您离开 using 块时会自动关闭
  • 此外,明确关闭您的流是个好主意。你正在关闭你的流阅读器,但不是作者。

标签: c# streamreader streamwriter


【解决方案1】:

写完后需要Flush()StreamWriter。

默认情况下,StreamWriter 是缓冲的,这意味着它在收到 Flush() 或 Close() 调用之前不会输出。

你也可以尝试像这样关闭它:

sw.Close();  //or tw.Flush();

你也可以看看StreamWriter.AutoFlush Property

获取或设置一个指示 StreamWriter 是否将刷新的值 每次调用后它的缓冲区到底层流 StreamWriter.Write.

现在非常流行和推荐的另一个选项是使用 using statement 来处理它。

提供方便的语法,确保正确使用 IDisposable 对象。

例子:

using(var sr = new StreamReader("C:\\Temp1\\test1.txt"))
using(var sw = new StreamWriter("C:\\Temp2\\test2.txt"))
{
   ...
}

【讨论】:

  • 或者事件更好,关闭它
  • 您应该在 StreamReader 和 StreamWriter 上使用“using”语句。
  • @tdbeckett:- 是的,这就是标准和推荐的方式!感谢您指出!
【解决方案2】:

您需要关闭 StreamWriter。像这样:

using(var sr = new StreamReader("..."))
using(var sw = new StreamWriter("..."))
{
   ...
}

即使抛出异常,这也会关闭流。

【讨论】:

  • +1 表示最佳“最佳实践”。所有 IDisposable 对象都应该(几乎总是)与 using 语句配对。
  • 是的,应该使用 using 语句——无需显式调用 Flush/Close,这样更简洁。
  • 还可以通过结合 readline 和比较来清理循环 while ((line = sr.ReadLine()) != null) { //将行写入控制台窗口 Console.WriteLine(线);诠释 myVal = 3; for (int i = 0; i
猜你喜欢
  • 1970-01-01
  • 2012-11-01
  • 2017-03-23
  • 2018-07-13
  • 1970-01-01
  • 2016-02-03
  • 2013-11-02
  • 1970-01-01
  • 2017-11-27
相关资源
最近更新 更多