Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAdam Sawicki
15,967 PointsClosing Stream?
Hey,
I wonder if @craigdennis should close the InpuStreamReader after reading the line or It isn't necessary in this case?
regards
2 Answers
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsHi. I think you should close BufferedReader
but not InpuStreamReader
inside.
So after quit you can put bufferedReader.close()
. For more on that topic, see for example http://stackoverflow.com/questions/1388602/do-i-need-to-close-both-filereader-and-bufferedreader
Craig Dennis
Treehouse TeacherGreat question! You are right, I really should be closing that stream.
Ideally that prompting would be in a class of itself and implement the AutoCloseable interface, then we could use the try-with-resources style. Or we could also create a reader in the try there and pass it as an argument to each method. Both solutions seem a little out of scope, but great idea spotting a bug!
The best idea is to probably add a finally clause to that try statement that closes the reader. The problem is if someone else calls the run method, the reader would be closed.
Thanks for pointing this out!