Skip to content

Commit fc3e627

Browse files
committed
Try with resources
1 parent 381265c commit fc3e627

1 file changed

Lines changed: 12 additions & 25 deletions

File tree

src/test/java/org/apache/commons/io/output/FileWriterWithEncodingTest.java

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,12 @@ public void sameEncoding_string_CharsetEncoder_constructor() throws Exception {
100100
}
101101

102102
private void successfulRun(final FileWriterWithEncoding fw21) throws Exception {
103-
FileWriter fw1 = null;
104-
FileWriterWithEncoding fw2 = null;
105-
try {
106-
fw1 = new FileWriter(file1); // default encoding
107-
fw2 = fw21;
103+
try (
104+
FileWriter fw1 = new FileWriter(file1); // default encoding
105+
FileWriterWithEncoding fw2 = fw21;
106+
){
108107
writeTestPayload(fw1, fw2);
109108
checkFile(file1, file2);
110-
111-
} finally {
112-
IOUtils.closeQuietly(fw1);
113-
IOUtils.closeQuietly(fw2);
114109
}
115110
assertTrue(file1.exists());
116111
assertTrue(file2.exists());
@@ -119,11 +114,10 @@ private void successfulRun(final FileWriterWithEncoding fw21) throws Exception {
119114
@Test
120115
public void testDifferentEncoding() throws Exception {
121116
if (Charset.isSupported("UTF-16BE")) {
122-
FileWriter fw1 = null;
123-
FileWriterWithEncoding fw2 = null;
124-
try {
125-
fw1 = new FileWriter(file1); // default encoding
126-
fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
117+
try (
118+
FileWriter fw1 = new FileWriter(file1); // default encoding
119+
FileWriterWithEncoding fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
120+
){
127121
writeTestPayload(fw1, fw2);
128122
try {
129123
checkFile(file1, file2);
@@ -132,19 +126,15 @@ public void testDifferentEncoding() throws Exception {
132126
// success
133127
}
134128

135-
} finally {
136-
IOUtils.closeQuietly(fw1);
137-
IOUtils.closeQuietly(fw2);
138129
}
139130
assertTrue(file1.exists());
140131
assertTrue(file2.exists());
141132
}
142133
if (Charset.isSupported("UTF-16LE")) {
143-
FileWriter fw1 = null;
144-
FileWriterWithEncoding fw2 = null;
145-
try {
146-
fw1 = new FileWriter(file1); // default encoding
147-
fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
134+
try (
135+
FileWriter fw1 = new FileWriter(file1); // default encoding
136+
FileWriterWithEncoding fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
137+
){
148138
writeTestPayload(fw1, fw2);
149139
try {
150140
checkFile(file1, file2);
@@ -153,9 +143,6 @@ public void testDifferentEncoding() throws Exception {
153143
// success
154144
}
155145

156-
} finally {
157-
IOUtils.closeQuietly(fw1);
158-
IOUtils.closeQuietly(fw2);
159146
}
160147
assertTrue(file1.exists());
161148
assertTrue(file2.exists());

0 commit comments

Comments
 (0)