Skip to content

Commit cf139a3

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 9f68c1f commit cf139a3

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,20 +221,20 @@ public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncod
221221
* @throws NullPointerException if the file or encoding is null
222222
* @throws IOException if an error occurs
223223
*/
224-
private static Writer initWriter(final File file, final Object encoding, final boolean append) throws IOException {
224+
private static Writer initWriter(final File file, final Object encoding, final boolean append) throws IOException {
225225
Objects.requireNonNull(file, "file");
226226
Objects.requireNonNull(encoding, "encoding");
227227
OutputStream stream = null;
228228
final boolean fileExistedAlready = file.exists();
229229
try {
230-
stream = Files.newOutputStream(file.toPath(), append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
230+
stream = Files.newOutputStream(file.toPath(), append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
231231
if (encoding instanceof Charset) {
232-
return new OutputStreamWriter(stream, (Charset)encoding);
232+
return new OutputStreamWriter(stream, (Charset) encoding);
233233
}
234234
if (encoding instanceof CharsetEncoder) {
235-
return new OutputStreamWriter(stream, (CharsetEncoder)encoding);
235+
return new OutputStreamWriter(stream, (CharsetEncoder) encoding);
236236
}
237-
return new OutputStreamWriter(stream, (String)encoding);
237+
return new OutputStreamWriter(stream, (String) encoding);
238238
} catch (final IOException | RuntimeException ex) {
239239
try {
240240
IOUtils.close(stream);

0 commit comments

Comments
 (0)