FileWriterWithEncoding for an existing file no longer truncates
the file.
This commit is a much simpler fix than PR 251. The test from the PR is
the same, just reformatted.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -50,6 +50,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="fix" due-to="Gary Gregory">
Fix Javadoc links to the JRE Javadoc 8.
</action>
+ <action issue="IO-744" dev="ggregory" type="fix" due-to="RBRi, Gary Gregory">
+ FileWriterWithEncoding for an existing file no longer truncates the file. #251.
+ </action>
<!-- ADD -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add BrokenReader.INSTANCE.

MODIFY
src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
@@ -227,7 +227,7 @@ private static Writer initWriter(final File file, final Object encoding, final b
OutputStream stream = null;
final boolean fileExistedAlready = file.exists();
try {
- stream = Files.newOutputStream(file.toPath(), append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE);
+ stream = Files.newOutputStream(file.toPath(), append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
if (encoding instanceof Charset) {
return new OutputStreamWriter(stream, (Charset)encoding);
}

MODIFY
src/test/java/org/apache/commons/io/output/FileWriterWithEncodingTest.java
src/test/java/org/apache/commons/io/output/FileWriterWithEncodingTest.java
import static org.apache.commons.io.test.TestUtils.checkFile;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@@ -99,10 +100,8 @@ public void sameEncoding_string_CharsetEncoder_constructor() throws Exception {
}
private void successfulRun(final FileWriterWithEncoding fw21) throws Exception {
- try (
- FileWriter fw1 = new FileWriter(file1); // default encoding
- FileWriterWithEncoding fw2 = fw21
- ){
+ try (FileWriter fw1 = new FileWriter(file1); // default encoding
+ FileWriterWithEncoding fw2 = fw21) {
writeTestPayload(fw1, fw2);
checkFile(file1, file2);
}
@@ -113,10 +112,8 @@ private void successfulRun(final FileWriterWithEncoding fw21) throws Exception {
@Test
public void testDifferentEncoding() throws Exception {
if (Charset.isSupported("UTF-16BE")) {
- try (
- FileWriter fw1 = new FileWriter(file1); // default encoding
- FileWriterWithEncoding fw2 = new FileWriterWithEncoding(file2, defaultEncoding)
- ){
+ try (FileWriter fw1 = new FileWriter(file1); // default encoding
+ FileWriterWithEncoding fw2 = new FileWriterWithEncoding(file2, defaultEncoding)) {
writeTestPayload(fw1, fw2);
try {
checkFile(file1, file2);
@@ -130,10 +127,8 @@ public void testDifferentEncoding() throws Exception {
assertTrue(file2.exists());
}
if (Charset.isSupported("UTF-16LE")) {
- try (
- FileWriter fw1 = new FileWriter(file1); // default encoding
- FileWriterWithEncoding fw2 = new FileWriterWithEncoding(file2, defaultEncoding)
- ){
+ try (FileWriter fw1 = new FileWriter(file1); // default encoding
+ FileWriterWithEncoding fw2 = new FileWriterWithEncoding(file2, defaultEncoding)) {
writeTestPayload(fw1, fw2);
try {
checkFile(file1, file2);
@@ -170,9 +165,9 @@ private void writeTestPayload(final FileWriter fw1, final FileWriterWithEncoding
@Test
public void constructor_File_encoding_badEncoding() {
assertThrows(IOException.class, () -> {
- try (
- Writer writer = new FileWriterWithEncoding(file1, "BAD-ENCODE")
- ){ }
+ try (Writer writer = new FileWriterWithEncoding(file1, "BAD-ENCODE")) {
+ // empty
+ }
});
assertFalse(file1.exists());
}
@@ -180,9 +175,9 @@ public void constructor_File_encoding_badEncoding() {
@Test
public void constructor_File_directory() {
assertThrows(IOException.class, () -> {
- try (
- Writer writer = new FileWriterWithEncoding(temporaryFolder, defaultEncoding)
- ){ }
+ try (Writer writer = new FileWriterWithEncoding(temporaryFolder, defaultEncoding)) {
+ // empty
+ }
});
assertFalse(file1.exists());
}
@@ -190,9 +185,9 @@ public void constructor_File_directory() {
@Test
public void constructor_File_nullFile() {
assertThrows(NullPointerException.class, () -> {
- try (
- Writer writer = new FileWriterWithEncoding((File) null, defaultEncoding)
- ){ }
+ try (Writer writer = new FileWriterWithEncoding((File) null, defaultEncoding)) {
+ // empty
+ }
});
assertFalse(file1.exists());
}
@@ -200,9 +195,9 @@ public void constructor_File_nullFile() {
@Test
public void constructor_fileName_nullFile() {
assertThrows(NullPointerException.class, () -> {
- try (
- Writer writer = new FileWriterWithEncoding((String) null, defaultEncoding)
- ){ }
+ try (Writer writer = new FileWriterWithEncoding((String) null, defaultEncoding)) {
+ // empty
+ }
});
assertFalse(file1.exists());
}
@@ -213,7 +208,22 @@ public void sameEncoding_null_Charset_constructor() throws Exception {
successfulRun(new FileWriterWithEncoding(file2, (Charset) null));
fail();
} catch (final NullPointerException ignore) {
+ // empty
+ }
+ }
+ @Test
+ public void constructor_File_existingFile_withContent() throws Exception {
+ try (FileWriter fw1 = new FileWriter(file1);) {
+ fw1.write(textContent);
+ fw1.write(65);
}
+ assertEquals(1025, file1.length());
+
+ try (FileWriterWithEncoding fw1 = new FileWriterWithEncoding(file1, defaultEncoding)) {
+ fw1.write("ABcd");
+ }
+
+ assertEquals(4, file1.length());
}
}

[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.