I have the following code to read and write a single line to a text file. Can we assign null to the reader and the writer objects at the end of the processing? or should I invoke Close() method? Thank you.
FileInfo JFile = new FileInfo(@"C:\test.txt");
using (FileStream JStream = JFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
{
StreamReader reader = null;
StreamWriter writer = null;
try
{
int n;
reader = new StreamReader(JStream);
string line = reader.ReadLine();
int.TryParse(line, out n);
n = n + 1;
writer = new StreamWriter(JStream);
JStream.Seek(0, SeekOrigin.Begin);
writer.WriteLine(n);
writer.Flush();
}
开发者_如何学Python catch (Exception x)
{
string message = string.Format("Error while processing the file. {0}", x.Message);
System.Windows.Forms.MessageBox.Show(message);
}
reader = null;
writer = null;
}
Certain things need to happen when closing a file. For instance, when your program access a file, and creates a handle on it. .Close()
-ing the stream allows the framework to immediately release the handle and other similar resources.
When you set it to null
, you are relying on the garbage collector to do this important functionality for you. The GC might clean it all up now, or it might not. You dont know when it is going to happen. If your program crashes, it might never release the handle at all.
Its always a good idea to .Close()
the stream yourself as soon as youre done.
You should be calling the Close method so that all the underlying unmanaged resources are freed. Setting the object to null, does not free up the resources. For details see http://msdn.microsoft.com/en-us/library/system.io.stream.close.aspx
Update: Since your code is inside using statement the Dispose method would be automatically called once execution is complete inside using statement.
When you set the object to null, you tell the CLR that you no longer need it and it is scheduled for Garbage Collection. The problem you will have is that the CLR's hooks into the file you have opened are not released until your program ends and the CLR is closed. When you invoke the Close() method, the file resources are cleaned up and then the bits and pieces are all scheduled for Garbage Collection properly.
if you open the streamreader in a using block it will take care of that for you as well but you loose some of the fine grained error handling you get with a try/catch/finally block.
Both the StreamReader
and the StreamWriter
implement IDisposable
, which is their way of saying
"When you are done with me, please call this method (
Dispose()
) so I know you're done with me and I can clean myself up".
(This is also called Deterministic Finalization)
So you need to call Dispose
on those, or wrap them in a using
statement, which just does the same thing for you. In the case of these two classes, Dispose
will internally call Close
if it's not already closed.
You actually want to call Close() or better yet Dispose() to ensure all contents are flushed. You risk not having the buffer written if you do not explicitly close/dispose the Writer.
I think the best solution will be to use the using
statment (like you did in FileStream
).
It will take care of all the cleaning.
精彩评论