2222import java .io .IOException ;
2323import java .io .OutputStream ;
2424import java .io .OutputStreamWriter ;
25+ import java .io .UnsupportedEncodingException ;
2526import java .io .Writer ;
27+ import java .nio .charset .Charset ;
28+ import java .nio .charset .UnsupportedCharsetException ;
2629
30+ import org .apache .commons .io .Charsets ;
2731import org .apache .commons .io .FileUtils ;
2832import org .apache .commons .io .IOUtils ;
2933
@@ -130,7 +134,7 @@ public LockableFileWriter(File file, boolean append) throws IOException {
130134 * @throws IOException in case of an I/O error
131135 */
132136 public LockableFileWriter (File file , boolean append , String lockDir ) throws IOException {
133- this (file , null , append , lockDir );
137+ this (file , Charset . defaultCharset () , append , lockDir );
134138 }
135139
136140 /**
@@ -140,6 +144,22 @@ public LockableFileWriter(File file, boolean append, String lockDir) throws IOEx
140144 * @param encoding the encoding to use, null means platform default
141145 * @throws NullPointerException if the file is null
142146 * @throws IOException in case of an I/O error
147+ * @since 2.3
148+ */
149+ public LockableFileWriter (File file , Charset encoding ) throws IOException {
150+ this (file , encoding , false , null );
151+ }
152+
153+ /**
154+ * Constructs a LockableFileWriter with a file encoding.
155+ *
156+ * @param file the file to write to, not null
157+ * @param encoding the encoding to use, null means platform default
158+ * @throws NullPointerException if the file is null
159+ * @throws IOException in case of an I/O error
160+ * @throws UnsupportedCharsetException
161+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
162+ * supported.
143163 */
144164 public LockableFileWriter (File file , String encoding ) throws IOException {
145165 this (file , encoding , false , null );
@@ -154,8 +174,9 @@ public LockableFileWriter(File file, String encoding) throws IOException {
154174 * @param lockDir the directory in which the lock file should be held
155175 * @throws NullPointerException if the file is null
156176 * @throws IOException in case of an I/O error
177+ * @since 2.3
157178 */
158- public LockableFileWriter (File file , String encoding , boolean append ,
179+ public LockableFileWriter (File file , Charset encoding , boolean append ,
159180 String lockDir ) throws IOException {
160181 super ();
161182 // init file to create/append
@@ -183,6 +204,24 @@ public LockableFileWriter(File file, String encoding, boolean append,
183204 out = initWriter (file , encoding , append );
184205 }
185206
207+ /**
208+ * Constructs a LockableFileWriter with a file encoding.
209+ *
210+ * @param file the file to write to, not null
211+ * @param encoding the encoding to use, null means platform default
212+ * @param append true if content should be appended, false to overwrite
213+ * @param lockDir the directory in which the lock file should be held
214+ * @throws NullPointerException if the file is null
215+ * @throws IOException in case of an I/O error
216+ * @throws UnsupportedCharsetException
217+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
218+ * supported.
219+ */
220+ public LockableFileWriter (File file , String encoding , boolean append ,
221+ String lockDir ) throws IOException {
222+ this (file , Charsets .toCharset (encoding ), append , lockDir );
223+ }
224+
186225 //-----------------------------------------------------------------------
187226 /**
188227 * Tests that we can write to the lock directory.
@@ -227,7 +266,7 @@ private void createLock() throws IOException {
227266 * @return The initialised writer
228267 * @throws IOException if an error occurs
229268 */
230- private Writer initWriter (File file , String encoding , boolean append ) throws IOException {
269+ private Writer initWriter (File file , Charset encoding , boolean append ) throws IOException {
231270 boolean fileExistedAlready = file .exists ();
232271 OutputStream stream = null ;
233272 Writer writer = null ;
0 commit comments