8232003: (fs) Files.write can leak file descriptor in the exception case
Summary: Be sure to close the leaked OutputStream in all cases
Reviewed-by: alanb, bpb, clanger
--- a/src/java.base/share/classes/java/nio/file/Files.java Wed Oct 09 10:15:58 2019 +0000
+++ b/src/java.base/share/classes/java/nio/file/Files.java Tue Oct 08 15:30:46 2019 +0200
@@ -3550,8 +3550,8 @@
// ensure lines is not null before opening file
Objects.requireNonNull(lines);
CharsetEncoder encoder = cs.newEncoder();
- OutputStream out = newOutputStream(path, options);
- try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoder))) {
+ try (OutputStream out = newOutputStream(path, options);
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoder))) {
for (CharSequence line: lines) {
writer.append(line);
writer.newLine();