Reputation: 21
I'm writing a chat application in Java for didactical purposes. Of course, I met a lot of problems as I'm not an experienced programmer.
Basically my question is: Do I have to close EVERY resource (BufferedReader/Writer etc.) after the use? Even if I know I will probably reuse it?
For example: the client that waits for the user to input text, can reuse the same BufferedWriter or has to create it every time the user inputs something and then close it again?
Upvotes: 2
Views: 61
Reputation: 20185
If you want to check one and the same resource multiple times, just cloes it, when you do not use it anymore. You can use try-with-resources for this purpose:
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.readLine();
}
catch (IOException e) {...}
Upvotes: 1