简体   繁体   English

对流作家的基本了解

[英]basic understanding of stream writer

HI, 嗨,

My question has to do with a very basic understanding of Writing data to using a StreamWriter. 我的问题与对使用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. 使用文件名初始化writer对象时,它所具有的只是指向文件的指针。

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? 但是,当我们将任何字符串写入writer对象时,它实际上是否加载整个文件,读取其内容,将字符串追加到末尾然后关闭句柄?

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. 我之所以这么问是因为,我遇到了一个应用程序,该应用程序可能经常每半秒写入一次文件,并且文件大小增加到大约1 GB,并且仍然继续写入文件。 (logging) (正在记录)

Do you think this could have resulted in a CPU usage of 100 % ? 您是否认为这可能导致100%的CPU使用率?

Please let me know if my question is unclear? 如果我的问题不清楚,请告诉我?

Thanks in advance. 提前致谢。

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. 框架打开文件后,它将执行FileStream.Seek操作以将文件指针定位到文件末尾。 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 . 当您调用CloseDispose时,手柄将关闭。 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.) (为方便起见,请注意,您可以利用C# using语句创建作用域,在此作用域中,对Dispose的调用由编译器在退出作用域时进行处理。)

every half a second to a file 每半秒钟到一个文件

That doesn't sound frequent enough to load the machine at 100%. 听起来不够频繁,无法以100%的速度加载计算机。 Especially since disk I/O mainly consists of waiting on the disk, and this kind of wait does not contribute to CPU usage. 特别是由于磁盘I / O主要由在磁盘上等待组成,这种等待不会增加CPU使用率。 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. 当您随机暂停应用程序时,很有可能将一个耗时的方法放在堆栈上。

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. true参数是告诉它附加到文件中。
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. 这就是所谓的“流”的意思,这意味着如果您采用一种方式,那么您将采用一种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM