What should be done to avoid resource leaks when writing to a file?

Last Updated :
Discuss
Comments

What should be done to avoid resource leaks when writing to a file?

Call flush() only

Use BufferedReader

Use FileWriter without closing

Use try-with-resources

Share your thoughts in the comments