流编写器的基本理解

发布于 2024-10-02 16:45:05 字数 581 浏览 2 评论 0原文

您好,

我的问题与使用 StreamWriter 写入数据的基本理解有关。 如果考虑以下代码:

            StreamWriter writer = new StreamWriter(@"C:\TEST.XML");
            writer.WriteLine("somestring");
            writer.Flush();
            writer.Close();

当使用文件名初始化 writer 对象时,它所拥有的只是一个指向文件的指针。

但是,当我们向 writer 对象写入任何字符串时,它实际上会加载整个文件,读取其内容,将字符串附加到末尾,然后关闭句柄吗?

我希望这不是一个愚蠢的问题。 我问这个问题是因为,我遇到一个应用程序,它频繁地写入文件,大约每半秒一次,并且文件大小增加到大约 1 GB,并且它仍然继续写入文件。 (日志记录)

您认为这会导致 CPU 使用率达到 100% 吗?

如果我的问题不清楚,请告诉我?

提前致谢。

HI,

My question has to do with a very basic understanding of Writing data to using a StreamWriter.
If you consider the following code:

            StreamWriter writer = new StreamWriter(@"C:\TEST.XML");
            writer.WriteLine("somestring");
            writer.Flush();
            writer.Close();

When the writer object is initialized, with the filename, all it has is a pointer to the file.

However when we write any string to the writer object, does it actually LOAD the whole file, read its contents, append the string towards the end and then close the handle?

I hope its not a silly questions.
I ask this because, I came across an application that writes frequently probably every half a second to a file, and the file size increased to about 1 GB, and it still continuted to write to the file. (logging)

Do you think this could have resulted in a CPU usage of 100 % ?

Please let me know if my question is unclear?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

维持三分热 2024-10-09 16:45:05

它实际上加载整个文件,读取其内容

框架打开文件后,它将执行 FileStream.Seek 操作将文件指针定位到文件末尾。这是操作系统支持的,不需要读取或写入任何文件数据。

然后关闭手柄

当您调用CloseDispose 时,句柄将被关闭。两者是等价的。 (请注意,为方便起见,您可以利用 C# 使用 语句来创建一个范围,其中对 Dispose 的调用由编译器在退出范围时处理。)

每半秒一个文件

这听起来不够频繁,无法以 100% 加载机器。特别是因为磁盘 I/O 主要是在磁盘上等待,而这种等待不会影响 CPU 的使用。使用分析器查看您的应用程序将时间花在哪里。或者,您可以尝试的一种简单技术是在调试器下运行,单击暂停,然后检查线程的调用堆栈。当您随机暂停应用程序时,消耗大量时间的方法很可能会出现在堆栈中。

does it actually LOAD the whole file, read its contents

After the framework opens the file, it will perform a FileStream.Seek operation to position the file pointer to the end of the file. This is supported by the operating system, and does not require reading or writing any file data.

and then close the handle

The handle is closed when you call Close or Dispose. Both are equivalent. (Note for convenience that you can take advantage of the C# using statement to create a scope where the call to Dispose is handled by the compiler on exiting the scope.)

every half a second to a file

That doesn't sound frequent enough to load the machine at 100%. Especially since disk I/O mainly consists of waiting on the disk, and this kind of wait does not contribute to CPU usage. Use a profiler to see where your application is spending its time. Alternatively, a simple technique that you might try is to run under the debugger, click pause, and examine the call stacks of your threads. There is a good chance that a method that is consuming a lot of time will be on a stack when you randomly pause the application.

甜是你 2024-10-09 16:45:05

您上面提供的代码将覆盖文件的内容,因此不需要预先加载文件。
尽管如此,您可以通过以下方式附加到文件:

StreamWriter writer = new StreamWriter(@"C:\TEST.XML", true);

true 参数是告诉它附加到文件。
而且,在附加到文件之前,它不会将整个文件加载到内存中。
这就是为什么这被称为“流”,这意味着如果你走单向,你就走单向。

The code you provided above will overwrite the content of the file, so it has no need to load the file upfront.
Nonetheless, you can append to a file by saying:

StreamWriter writer = new StreamWriter(@"C:\TEST.XML", true);

The true parameter is to tell it to append to the file.
And still, it does not load the entire file in memory before it appends to it.
That's what makes this called a "stream", which means if you're going one way, you're going one way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文