In C#, I almost always use the using
pattern when working with stream objects. For example:
using (Stream stream = new MemoryStream())
{
// do stuff
}
By using the using
block, we ensure that dispose is called on the stream immediately after that code bock executes.
I know Java doesn't have the equivalent of a using
keyword, but my question is that when working with an object like a FileOutputStream
in Java, do we need to do any housekeeping to make sure it gets disposed? I was looking at this code example, and I noticed they don't do any.
I just wondered what the best practice was for Java in handling disposing streams, or if it's good enough to let the garbage collector handle it.