FileWriterWithEncoding for an existing file no longer truncates
the file.
Bring in test from PR and add a fix.
MODIFY
src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.file.Files;
+import java.nio.file.OpenOption;
import java.nio.file.StandardOpenOption;
import java.util.Objects;
/**
* Writer of files that allows the encoding to be set.
* <p>
- * This class provides a simple alternative to {@code FileWriter}
- * that allows an encoding to be set. Unfortunately, it cannot subclass
- * {@code FileWriter}.
+ * This class provides a simple alternative to {@code FileWriter} that allows an encoding to be set. Unfortunately, it
+ * cannot subclass {@code FileWriter}.
* </p>
* <p>
* By default, the file will be overwritten, but this may be changed to append.
* </p>
* <p>
- * The encoding must be specified using either the name of the {@link Charset},
- * the {@link Charset}, or a {@link CharsetEncoder}. If the default encoding
- * is required then use the {@link java.io.FileWriter} directly, rather than
- * this implementation.
+ * The encoding must be specified using either the name of the {@link Charset}, the {@link Charset}, or a
+ * {@link CharsetEncoder}. If the default encoding is required then use the {@link java.io.FileWriter} directly, rather
+ * than this implementation.
* </p>
*
* @since 1.4
@@ -68,7 +67,9 @@ 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, StandardOpenOption.TRUNCATE_EXISTING);
+ stream = Files.newOutputStream(file.toPath(), append ?
+ new OpenOption[] {StandardOpenOption.APPEND} :
+ new OpenOption[] {StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING});
if (encoding == null || encoding instanceof Charset) {
return new OutputStreamWriter(stream, Charsets.toCharset((Charset) encoding));
}
@@ -138,8 +139,7 @@ public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncod
* @throws NullPointerException if the file is null.
* @throws IOException in case of an I/O error.
*/
- public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncoder, final boolean append)
- throws IOException {
+ public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncoder, final boolean append) throws IOException {
this.out = initWriter(file, charsetEncoder, append);
}
@@ -189,8 +189,7 @@ public FileWriterWithEncoding(final String fileName, final Charset charset) thro
* @throws NullPointerException if the file name or encoding is null
* @throws IOException in case of an I/O error
*/
- public FileWriterWithEncoding(final String fileName, final Charset charset, final boolean append)
- throws IOException {
+ public FileWriterWithEncoding(final String fileName, final Charset charset, final boolean append) throws IOException {
this(new File(fileName), charset, append);
}
@@ -215,8 +214,7 @@ public FileWriterWithEncoding(final String fileName, final CharsetEncoder encodi
* @throws NullPointerException if the file name or encoding is null
* @throws IOException in case of an I/O error
*/
- public FileWriterWithEncoding(final String fileName, final CharsetEncoder charsetEncoder, final boolean append)
- throws IOException {
+ public FileWriterWithEncoding(final String fileName, final CharsetEncoder charsetEncoder, final boolean append) throws IOException {
this(new File(fileName), charsetEncoder, append);
}
@@ -241,13 +239,13 @@ public FileWriterWithEncoding(final String fileName, final String charsetName) t
* @throws NullPointerException if the file name or encoding is null
* @throws IOException in case of an I/O error
*/
- public FileWriterWithEncoding(final String fileName, final String charsetName, final boolean append)
- throws IOException {
+ public FileWriterWithEncoding(final String fileName, final String charsetName, final boolean append) throws IOException {
this(new File(fileName), charsetName, append);
}
/**
* Closes the stream.
+ *
* @throws IOException if an I/O error occurs.
*/
@Override
@@ -257,6 +255,7 @@ public void close() throws IOException {
/**
* Flushes the stream.
+ *
* @throws IOException if an I/O error occurs.
*/
@Override
@@ -266,6 +265,7 @@ public void flush() throws IOException {
/**
* Writes the characters from an array.
+ *
* @param chr the characters to write
* @throws IOException if an I/O error occurs.
*/
@@ -276,6 +276,7 @@ public void write(final char[] chr) throws IOException {
/**
* Writes the specified characters from an array.
+ *
* @param chr the characters to write
* @param st The start offset
* @param end The number of characters to write
@@ -288,6 +289,7 @@ public void write(final char[] chr, final int st, final int end) throws IOExcept
/**
* Writes a character.
+ *
* @param idx the character to write
* @throws IOException if an I/O error occurs.
*/
@@ -298,6 +300,7 @@ public void write(final int idx) throws IOException {
/**
* Writes the characters from a string.
+ *
* @param str the string to write
* @throws IOException if an I/O error occurs.
*/
@@ -308,6 +311,7 @@ public void write(final String str) throws IOException {
/**
* Writes the specified characters from a string.
+ *
* @param str the string to write
* @param st The start offset
* @param end The number of characters to write

MODIFY
src/test/java/org/apache/commons/io/output/FileWriterWithEncodingTest.java
src/test/java/org/apache/commons/io/output/FileWriterWithEncodingTest.java
@@ -105,6 +105,20 @@ public void constructor_fileName_nullFile() {
assertFalse(file1.exists());
}
+ @Test
+ public void constructorAppend_File_existingFile_withContent() throws Exception {
+ try (FileWriter fw1 = new FileWriter(file1)) {
+ fw1.write("ABcd");
+ }
+ assertEquals(4, file1.length());
+
+ try (FileWriterWithEncoding fw1 = new FileWriterWithEncoding(file1, defaultEncoding, true)) {
+ fw1.write("XyZ");
+ }
+
+ assertEquals(7, file1.length());
+ }
+
@Test
public void sameEncoding_Charset_constructor() throws Exception {
try (final FileWriterWithEncoding writer = new FileWriterWithEncoding(file2, Charset.defaultCharset())) {

[IO-744] FileWriterWithEncoding for an existing file no longer truncates
the file. #251.

Bring in test from PR #252 and add a fix.