Skip to content

Commit 41dc1a2

Browse files
author
Gary Gregory
committed
[IO-744] FileWriterWithEncoding for an existing file no longer truncates
the file. #251. Bring in test from PR #252 and add a fix.
1 parent 947b074 commit 41dc1a2

2 files changed

Lines changed: 73 additions & 55 deletions

File tree

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

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.nio.charset.Charset;
2525
import java.nio.charset.CharsetEncoder;
2626
import java.nio.file.Files;
27+
import java.nio.file.OpenOption;
2728
import java.nio.file.StandardOpenOption;
2829
import java.util.Objects;
2930

@@ -34,18 +35,16 @@
3435
/**
3536
* Writer of files that allows the encoding to be set.
3637
* <p>
37-
* This class provides a simple alternative to {@code FileWriter}
38-
* that allows an encoding to be set. Unfortunately, it cannot subclass
39-
* {@code FileWriter}.
38+
* This class provides a simple alternative to {@code FileWriter} that allows an encoding to be set. Unfortunately, it
39+
* cannot subclass {@code FileWriter}.
4040
* </p>
4141
* <p>
4242
* By default, the file will be overwritten, but this may be changed to append.
4343
* </p>
4444
* <p>
45-
* The encoding must be specified using either the name of the {@link Charset},
46-
* the {@link Charset}, or a {@link CharsetEncoder}. If the default encoding
47-
* is required then use the {@link java.io.FileWriter} directly, rather than
48-
* this implementation.
45+
* The encoding must be specified using either the name of the {@link Charset}, the {@link Charset}, or a
46+
* {@link CharsetEncoder}. If the default encoding is required then use the {@link java.io.FileWriter} directly, rather
47+
* than this implementation.
4948
* </p>
5049
*
5150
* @since 1.4
@@ -68,7 +67,9 @@ private static Writer initWriter(final File file, final Object encoding, final b
6867
OutputStream stream = null;
6968
final boolean fileExistedAlready = file.exists();
7069
try {
71-
stream = Files.newOutputStream(file.toPath(), append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
70+
stream = Files.newOutputStream(file.toPath(), append ?
71+
new OpenOption[] {StandardOpenOption.APPEND} :
72+
new OpenOption[] {StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING});
7273
if (encoding == null || encoding instanceof Charset) {
7374
return new OutputStreamWriter(stream, Charsets.toCharset((Charset) encoding));
7475
}
@@ -95,8 +96,8 @@ private static Writer initWriter(final File file, final Object encoding, final b
9596
/**
9697
* Constructs a FileWriterWithEncoding with a file encoding.
9798
*
98-
* @param file the file to write to, not null
99-
* @param charset the encoding to use, not null
99+
* @param file the file to write to, not null
100+
* @param charset the encoding to use, not null
100101
* @throws NullPointerException if the file or encoding is null
101102
* @throws IOException in case of an I/O error
102103
*/
@@ -107,9 +108,9 @@ public FileWriterWithEncoding(final File file, final Charset charset) throws IOE
107108
/**
108109
* Constructs a FileWriterWithEncoding with a file encoding.
109110
*
110-
* @param file the file to write to, not null.
111-
* @param encoding the name of the requested charset, null uses the default Charset.
112-
* @param append true if content should be appended, false to overwrite.
111+
* @param file the file to write to, not null.
112+
* @param encoding the name of the requested charset, null uses the default Charset.
113+
* @param append true if content should be appended, false to overwrite.
113114
* @throws NullPointerException if the file is null.
114115
* @throws IOException in case of an I/O error.
115116
*/
@@ -120,8 +121,8 @@ public FileWriterWithEncoding(final File file, final Charset encoding, final boo
120121
/**
121122
* Constructs a FileWriterWithEncoding with a file encoding.
122123
*
123-
* @param file the file to write to, not null
124-
* @param charsetEncoder the encoding to use, not null
124+
* @param file the file to write to, not null
125+
* @param charsetEncoder the encoding to use, not null
125126
* @throws NullPointerException if the file or encoding is null
126127
* @throws IOException in case of an I/O error
127128
*/
@@ -132,22 +133,21 @@ public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncod
132133
/**
133134
* Constructs a FileWriterWithEncoding with a file encoding.
134135
*
135-
* @param file the file to write to, not null.
136-
* @param charsetEncoder the encoding to use, null uses the default Charset.
137-
* @param append true if content should be appended, false to overwrite.
138-
* @throws NullPointerException if the file is null.
136+
* @param file the file to write to, not null.
137+
* @param charsetEncoder the encoding to use, null uses the default Charset.
138+
* @param append true if content should be appended, false to overwrite.
139+
* @throws NullPointerException if the file is null.
139140
* @throws IOException in case of an I/O error.
140141
*/
141-
public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncoder, final boolean append)
142-
throws IOException {
142+
public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncoder, final boolean append) throws IOException {
143143
this.out = initWriter(file, charsetEncoder, append);
144144
}
145145

146146
/**
147147
* Constructs a FileWriterWithEncoding with a file encoding.
148148
*
149-
* @param file the file to write to, not null
150-
* @param charsetName the name of the requested charset, not null
149+
* @param file the file to write to, not null
150+
* @param charsetName the name of the requested charset, not null
151151
* @throws NullPointerException if the file or encoding is null
152152
* @throws IOException in case of an I/O error
153153
*/
@@ -158,9 +158,9 @@ public FileWriterWithEncoding(final File file, final String charsetName) throws
158158
/**
159159
* Constructs a FileWriterWithEncoding with a file encoding.
160160
*
161-
* @param file the file to write to, not null.
162-
* @param charsetName the name of the requested charset, null uses the default Charset.
163-
* @param append true if content should be appended, false to overwrite.
161+
* @param file the file to write to, not null.
162+
* @param charsetName the name of the requested charset, null uses the default Charset.
163+
* @param append true if content should be appended, false to overwrite.
164164
* @throws NullPointerException if the file is null.
165165
* @throws IOException in case of an I/O error.
166166
*/
@@ -171,8 +171,8 @@ public FileWriterWithEncoding(final File file, final String charsetName, final b
171171
/**
172172
* Constructs a FileWriterWithEncoding with a file encoding.
173173
*
174-
* @param fileName the name of the file to write to, not null
175-
* @param charset the charset to use, not null
174+
* @param fileName the name of the file to write to, not null
175+
* @param charset the charset to use, not null
176176
* @throws NullPointerException if the file name or encoding is null
177177
* @throws IOException in case of an I/O error
178178
*/
@@ -183,22 +183,21 @@ public FileWriterWithEncoding(final String fileName, final Charset charset) thro
183183
/**
184184
* Constructs a FileWriterWithEncoding with a file encoding.
185185
*
186-
* @param fileName the name of the file to write to, not null
187-
* @param charset the encoding to use, not null
188-
* @param append true if content should be appended, false to overwrite
186+
* @param fileName the name of the file to write to, not null
187+
* @param charset the encoding to use, not null
188+
* @param append true if content should be appended, false to overwrite
189189
* @throws NullPointerException if the file name or encoding is null
190190
* @throws IOException in case of an I/O error
191191
*/
192-
public FileWriterWithEncoding(final String fileName, final Charset charset, final boolean append)
193-
throws IOException {
192+
public FileWriterWithEncoding(final String fileName, final Charset charset, final boolean append) throws IOException {
194193
this(new File(fileName), charset, append);
195194
}
196195

197196
/**
198197
* Constructs a FileWriterWithEncoding with a file encoding.
199198
*
200-
* @param fileName the name of the file to write to, not null
201-
* @param encoding the encoding to use, not null
199+
* @param fileName the name of the file to write to, not null
200+
* @param encoding the encoding to use, not null
202201
* @throws NullPointerException if the file name or encoding is null
203202
* @throws IOException in case of an I/O error
204203
*/
@@ -209,22 +208,21 @@ public FileWriterWithEncoding(final String fileName, final CharsetEncoder encodi
209208
/**
210209
* Constructs a FileWriterWithEncoding with a file encoding.
211210
*
212-
* @param fileName the name of the file to write to, not null
213-
* @param charsetEncoder the encoding to use, not null
214-
* @param append true if content should be appended, false to overwrite
211+
* @param fileName the name of the file to write to, not null
212+
* @param charsetEncoder the encoding to use, not null
213+
* @param append true if content should be appended, false to overwrite
215214
* @throws NullPointerException if the file name or encoding is null
216215
* @throws IOException in case of an I/O error
217216
*/
218-
public FileWriterWithEncoding(final String fileName, final CharsetEncoder charsetEncoder, final boolean append)
219-
throws IOException {
217+
public FileWriterWithEncoding(final String fileName, final CharsetEncoder charsetEncoder, final boolean append) throws IOException {
220218
this(new File(fileName), charsetEncoder, append);
221219
}
222220

223221
/**
224222
* Constructs a FileWriterWithEncoding with a file encoding.
225223
*
226-
* @param fileName the name of the file to write to, not null
227-
* @param charsetName the name of the requested charset, not null
224+
* @param fileName the name of the file to write to, not null
225+
* @param charsetName the name of the requested charset, not null
228226
* @throws NullPointerException if the file name or encoding is null
229227
* @throws IOException in case of an I/O error
230228
*/
@@ -235,85 +233,91 @@ public FileWriterWithEncoding(final String fileName, final String charsetName) t
235233
/**
236234
* Constructs a FileWriterWithEncoding with a file encoding.
237235
*
238-
* @param fileName the name of the file to write to, not null
239-
* @param charsetName the name of the requested charset, not null
240-
* @param append true if content should be appended, false to overwrite
236+
* @param fileName the name of the file to write to, not null
237+
* @param charsetName the name of the requested charset, not null
238+
* @param append true if content should be appended, false to overwrite
241239
* @throws NullPointerException if the file name or encoding is null
242240
* @throws IOException in case of an I/O error
243241
*/
244-
public FileWriterWithEncoding(final String fileName, final String charsetName, final boolean append)
245-
throws IOException {
242+
public FileWriterWithEncoding(final String fileName, final String charsetName, final boolean append) throws IOException {
246243
this(new File(fileName), charsetName, append);
247244
}
248245

249246
/**
250247
* Closes the stream.
248+
*
251249
* @throws IOException if an I/O error occurs.
252250
*/
253-
@Override
251+
@Override
254252
public void close() throws IOException {
255253
out.close();
256254
}
257255

258256
/**
259257
* Flushes the stream.
258+
*
260259
* @throws IOException if an I/O error occurs.
261260
*/
262-
@Override
261+
@Override
263262
public void flush() throws IOException {
264263
out.flush();
265264
}
266265

267266
/**
268267
* Writes the characters from an array.
268+
*
269269
* @param chr the characters to write
270270
* @throws IOException if an I/O error occurs.
271271
*/
272-
@Override
272+
@Override
273273
public void write(final char[] chr) throws IOException {
274274
out.write(chr);
275275
}
276276

277277
/**
278278
* Writes the specified characters from an array.
279+
*
279280
* @param chr the characters to write
280281
* @param st The start offset
281282
* @param end The number of characters to write
282283
* @throws IOException if an I/O error occurs.
283284
*/
284-
@Override
285+
@Override
285286
public void write(final char[] chr, final int st, final int end) throws IOException {
286287
out.write(chr, st, end);
287288
}
288289

289290
/**
290291
* Writes a character.
292+
*
291293
* @param idx the character to write
292294
* @throws IOException if an I/O error occurs.
293295
*/
294-
@Override
296+
@Override
295297
public void write(final int idx) throws IOException {
296298
out.write(idx);
297299
}
298300

299301
/**
300302
* Writes the characters from a string.
303+
*
301304
* @param str the string to write
302305
* @throws IOException if an I/O error occurs.
303306
*/
304-
@Override
307+
@Override
305308
public void write(final String str) throws IOException {
306309
out.write(str);
307310
}
308311

309312
/**
310313
* Writes the specified characters from a string.
314+
*
311315
* @param str the string to write
312316
* @param st The start offset
313317
* @param end The number of characters to write
314318
* @throws IOException if an I/O error occurs.
315319
*/
316-
@Override
320+
@Override
317321
public void write(final String str, final int st, final int end) throws IOException {
318322
out.write(str, st, end);
319323
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void constructor_File_directory() {
5959
});
6060
assertFalse(file1.exists());
6161
}
62-
62+
6363
@Test
6464
public void constructor_File_encoding_badEncoding() {
6565
assertThrows(IOException.class, () -> {
@@ -105,6 +105,20 @@ public void constructor_fileName_nullFile() {
105105
assertFalse(file1.exists());
106106
}
107107

108+
@Test
109+
public void constructorAppend_File_existingFile_withContent() throws Exception {
110+
try (FileWriter fw1 = new FileWriter(file1)) {
111+
fw1.write("ABcd");
112+
}
113+
assertEquals(4, file1.length());
114+
115+
try (FileWriterWithEncoding fw1 = new FileWriterWithEncoding(file1, defaultEncoding, true)) {
116+
fw1.write("XyZ");
117+
}
118+
119+
assertEquals(7, file1.length());
120+
}
121+
108122
@Test
109123
public void sameEncoding_Charset_constructor() throws Exception {
110124
try (final FileWriterWithEncoding writer = new FileWriterWithEncoding(file2, Charset.defaultCharset())) {

0 commit comments

Comments
 (0)