title: Is opening and closing a PrintWriter object repeatedly bad practice in Java?
body: <p>I'm trying to write a really simple Java logging class that can write to a log file so I can reuse it in my other projects. I extended the <code>PrintWriter</code> class so I could detect if the log file was open or not before writing to it.</p>
  
  <p>When the user is finished logging, they must call <code>.close()</code> on the logging object to close the file. When only integrated into a single-class application, this isn't that big of a deal, but now I'm trying to integrate this logger class into several different parent and child classes that will all reference the same log file, and without closing one reference, I won't be able to open another from a different class.</p>
  
  <p>Would it be terrible to call <code>.open()</code>, <code>.write(msg)</code>, <code>.close()</code> every time something is logged? It <em>would</em> eliminate the need to <code>open</code> and <code>close</code> the logger manually. Or <strong>is there a better solution?</strong></p>
  
  <p>I found <a href="https://stackoverflow.com/questions/15950267/best-practice-file-open-close-operations">this similar question</a> for C++, but the only answer was "in my experience..." and I'm kind of looking for a more in-depth answer.</p>