2727import java .nio .file .StandardOpenOption ;
2828import java .util .Objects ;
2929
30+ import org .apache .commons .io .Charsets ;
3031import org .apache .commons .io .FileUtils ;
3132import org .apache .commons .io .IOUtils ;
3233
@@ -149,11 +150,11 @@ public FileWriterWithEncoding(final File file, final String charsetName) throws
149150 /**
150151 * Constructs a FileWriterWithEncoding with a file encoding.
151152 *
152- * @param file the file to write to, not null
153- * @param charsetName the name of the requested charset, not null
154- * @param append true if content should be appended, false to overwrite
155- * @throws NullPointerException if the file or encoding is null
156- * @throws IOException in case of an I/O error
153+ * @param file the file to write to, not null.
154+ * @param charsetName the name of the requested charset, null uses the default Charset.
155+ * @param append true if content should be appended, false to overwrite.
156+ * @throws NullPointerException if the file is null.
157+ * @throws IOException in case of an I/O error.
157158 */
158159 public FileWriterWithEncoding (final File file , final String charsetName , final boolean append ) throws IOException {
159160 this .out = initWriter (file , charsetName , append );
@@ -174,11 +175,11 @@ public FileWriterWithEncoding(final File file, final Charset charset) throws IOE
174175 /**
175176 * Constructs a FileWriterWithEncoding with a file encoding.
176177 *
177- * @param file the file to write to, not null
178- * @param encoding the name of the requested charset, not null
179- * @param append true if content should be appended, false to overwrite
180- * @throws NullPointerException if the file or encoding is null
181- * @throws IOException in case of an I/O error
178+ * @param file the file to write to, not null.
179+ * @param encoding the name of the requested charset, null uses the default Charset.
180+ * @param append true if content should be appended, false to overwrite.
181+ * @throws NullPointerException if the file is null.
182+ * @throws IOException in case of an I/O error.
182183 */
183184 public FileWriterWithEncoding (final File file , final Charset encoding , final boolean append ) throws IOException {
184185 this .out = initWriter (file , encoding , append );
@@ -199,37 +200,34 @@ public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncod
199200 /**
200201 * Constructs a FileWriterWithEncoding with a file encoding.
201202 *
202- * @param file the file to write to, not null
203- * @param charsetEncoder the encoding to use, not null
204- * @param append true if content should be appended, false to overwrite
205- * @throws NullPointerException if the file or encoding is null
206- * @throws IOException in case of an I/O error
203+ * @param file the file to write to, not null.
204+ * @param charsetEncoder the encoding to use, null uses the default Charset.
205+ * @param append true if content should be appended, false to overwrite.
206+ * @throws NullPointerException if the file is null.
207+ * @throws IOException in case of an I/O error.
207208 */
208209 public FileWriterWithEncoding (final File file , final CharsetEncoder charsetEncoder , final boolean append )
209210 throws IOException {
210211 this .out = initWriter (file , charsetEncoder , append );
211212 }
212213
213214 /**
214- * Initialize the wrapped file writer.
215- * Ensure that a cleanup occurs if the writer creation fails.
215+ * Initializes the wrapped file writer. Ensure that a cleanup occurs if the writer creation fails.
216216 *
217- * @param file the file to be accessed
218- * @param encoding the encoding to use - may be Charset, CharsetEncoder or String
219- * @param append true to append
217+ * @param file the file to be accessed
218+ * @param encoding the encoding to use - may be Charset, CharsetEncoder or String, null uses the default Charset.
219+ * @param append true to append
220220 * @return the initialized writer
221- * @throws NullPointerException if the file or encoding is null
222221 * @throws IOException if an error occurs
223222 */
224223 private static Writer initWriter (final File file , final Object encoding , final boolean append ) throws IOException {
225224 Objects .requireNonNull (file , "file" );
226- Objects .requireNonNull (encoding , "encoding" );
227225 OutputStream stream = null ;
228226 final boolean fileExistedAlready = file .exists ();
229227 try {
230228 stream = Files .newOutputStream (file .toPath (), append ? StandardOpenOption .APPEND : StandardOpenOption .CREATE , StandardOpenOption .TRUNCATE_EXISTING );
231- if (encoding instanceof Charset ) {
232- return new OutputStreamWriter (stream , ( Charset ) encoding );
229+ if (encoding == null || encoding instanceof Charset ) {
230+ return new OutputStreamWriter (stream , Charsets . toCharset (( Charset ) encoding ) );
233231 }
234232 if (encoding instanceof CharsetEncoder ) {
235233 return new OutputStreamWriter (stream , (CharsetEncoder ) encoding );
0 commit comments