Skip to content

Commit 89675b6

Browse files
author
Gary Gregory
committed
[IO-744] FileWriterWithEncoding for an existing file no longer truncates
the file. #251. This commit is a much simpler fix than PR 251. The test from the PR is the same, just reformatted.
1 parent 96e3df8 commit 89675b6

3 files changed

Lines changed: 42 additions & 29 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ The <action> type attribute can be add,update,fix,remove.
5050
<action dev="ggregory" type="fix" due-to="Gary Gregory">
5151
Fix Javadoc links to the JRE Javadoc 8.
5252
</action>
53+
<action issue="IO-744" dev="ggregory" type="fix" due-to="RBRi, Gary Gregory">
54+
FileWriterWithEncoding for an existing file no longer truncates the file. #251.
55+
</action>
5356
<!-- ADD -->
5457
<action dev="ggregory" type="add" due-to="Gary Gregory">
5558
Add BrokenReader.INSTANCE.

src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private static Writer initWriter(final File file, final Object encoding, final b
227227
OutputStream stream = null;
228228
final boolean fileExistedAlready = file.exists();
229229
try {
230-
stream = Files.newOutputStream(file.toPath(), append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE);
230+
stream = Files.newOutputStream(file.toPath(), append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
231231
if (encoding instanceof Charset) {
232232
return new OutputStreamWriter(stream, (Charset)encoding);
233233
}

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

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.apache.commons.io.test.TestUtils.checkFile;
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
2122
import static org.junit.jupiter.api.Assertions.assertThrows;
2223
import static org.junit.jupiter.api.Assertions.assertTrue;
2324
import static org.junit.jupiter.api.Assertions.fail;
@@ -99,10 +100,8 @@ public void sameEncoding_string_CharsetEncoder_constructor() throws Exception {
99100
}
100101

101102
private void successfulRun(final FileWriterWithEncoding fw21) throws Exception {
102-
try (
103-
FileWriter fw1 = new FileWriter(file1); // default encoding
104-
FileWriterWithEncoding fw2 = fw21
105-
){
103+
try (FileWriter fw1 = new FileWriter(file1); // default encoding
104+
FileWriterWithEncoding fw2 = fw21) {
106105
writeTestPayload(fw1, fw2);
107106
checkFile(file1, file2);
108107
}
@@ -113,10 +112,8 @@ private void successfulRun(final FileWriterWithEncoding fw21) throws Exception {
113112
@Test
114113
public void testDifferentEncoding() throws Exception {
115114
if (Charset.isSupported("UTF-16BE")) {
116-
try (
117-
FileWriter fw1 = new FileWriter(file1); // default encoding
118-
FileWriterWithEncoding fw2 = new FileWriterWithEncoding(file2, defaultEncoding)
119-
){
115+
try (FileWriter fw1 = new FileWriter(file1); // default encoding
116+
FileWriterWithEncoding fw2 = new FileWriterWithEncoding(file2, defaultEncoding)) {
120117
writeTestPayload(fw1, fw2);
121118
try {
122119
checkFile(file1, file2);
@@ -130,10 +127,8 @@ public void testDifferentEncoding() throws Exception {
130127
assertTrue(file2.exists());
131128
}
132129
if (Charset.isSupported("UTF-16LE")) {
133-
try (
134-
FileWriter fw1 = new FileWriter(file1); // default encoding
135-
FileWriterWithEncoding fw2 = new FileWriterWithEncoding(file2, defaultEncoding)
136-
){
130+
try (FileWriter fw1 = new FileWriter(file1); // default encoding
131+
FileWriterWithEncoding fw2 = new FileWriterWithEncoding(file2, defaultEncoding)) {
137132
writeTestPayload(fw1, fw2);
138133
try {
139134
checkFile(file1, file2);
@@ -170,40 +165,40 @@ private void writeTestPayload(final FileWriter fw1, final FileWriterWithEncoding
170165
@Test
171166
public void constructor_File_encoding_badEncoding() {
172167
assertThrows(IOException.class, () -> {
173-
try (
174-
Writer writer = new FileWriterWithEncoding(file1, "BAD-ENCODE")
175-
){ }
176-
});
168+
try (Writer writer = new FileWriterWithEncoding(file1, "BAD-ENCODE")) {
169+
// empty
170+
}
171+
});
177172
assertFalse(file1.exists());
178173
}
179174

180175
@Test
181176
public void constructor_File_directory() {
182177
assertThrows(IOException.class, () -> {
183-
try (
184-
Writer writer = new FileWriterWithEncoding(temporaryFolder, defaultEncoding)
185-
){ }
186-
});
178+
try (Writer writer = new FileWriterWithEncoding(temporaryFolder, defaultEncoding)) {
179+
// empty
180+
}
181+
});
187182
assertFalse(file1.exists());
188183
}
189184

190185
@Test
191186
public void constructor_File_nullFile() {
192187
assertThrows(NullPointerException.class, () -> {
193-
try (
194-
Writer writer = new FileWriterWithEncoding((File) null, defaultEncoding)
195-
){ }
196-
});
188+
try (Writer writer = new FileWriterWithEncoding((File) null, defaultEncoding)) {
189+
// empty
190+
}
191+
});
197192
assertFalse(file1.exists());
198193
}
199194

200195
@Test
201196
public void constructor_fileName_nullFile() {
202197
assertThrows(NullPointerException.class, () -> {
203-
try (
204-
Writer writer = new FileWriterWithEncoding((String) null, defaultEncoding)
205-
){ }
206-
});
198+
try (Writer writer = new FileWriterWithEncoding((String) null, defaultEncoding)) {
199+
// empty
200+
}
201+
});
207202
assertFalse(file1.exists());
208203
}
209204

@@ -213,7 +208,22 @@ public void sameEncoding_null_Charset_constructor() throws Exception {
213208
successfulRun(new FileWriterWithEncoding(file2, (Charset) null));
214209
fail();
215210
} catch (final NullPointerException ignore) {
211+
// empty
212+
}
213+
}
216214

215+
@Test
216+
public void constructor_File_existingFile_withContent() throws Exception {
217+
try (FileWriter fw1 = new FileWriter(file1);) {
218+
fw1.write(textContent);
219+
fw1.write(65);
217220
}
221+
assertEquals(1025, file1.length());
222+
223+
try (FileWriterWithEncoding fw1 = new FileWriterWithEncoding(file1, defaultEncoding)) {
224+
fw1.write("ABcd");
225+
}
226+
227+
assertEquals(4, file1.length());
218228
}
219229
}

0 commit comments

Comments
 (0)