On 11/27/2012 2:38 PM, Lew wrote:
Tobiah wrote:I am opening a file for append using BufferedWriter. Each time the app is run, more data is added to the file. At first a made a little class representing the file and gave it a write() method that would take a string and send it out, then flush() the Writer. I was wondering when I should close the file, or whether I really needed to. Will java [sic] close the file for me when the app goes away? Right now, since the writes are few and far between, I'm just opening and closing the file each time I write a line to it, but that seems really awkward. What does "seems awkward" mean, let alone "really" awkward? What harm does "awkward" cause?
I think inelegant would have been a better choice of words but....
What's "awkward" is holding a resource like a file open for a really long time when you aren't using it.
Nice. I feel better already.
File handles are a finite resource. Like all such, you should manage them wisely. Assess the objective costs and benefits of the approach in the context where you use it. "Writes are few and far between", depending on what you mean by "few" and "far", could very well be a use case for closing the file between writes. Or not.
A record would be written to the file sometimes every few minutes, and at most every 10 seconds or so.
At what point does "not few" and "not far" become significant enough to justify not closing the file? Almost certainly the non-measurable degree of "awkwardness" is not relevant.
Thanks for clearing the waters. I feel a lot better about the way I wrote this bit of the code.
Cheers. Tobiah P.S., why did you put the [sic] in my quote? Was it because it would not be java that closes the file, but rather the OS, or rather I'm not even close? -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

