FileWriterWithEncodingpublic class FileWriterWithEncoding extends Writer Writer of files that allows the encoding to be set.
This class provides a simple alternative to FileWriter
that allows an encoding to be set. Unfortunately, it cannot subclass
FileWriter .
By default, the file will be overwritten, but this may be changed to append.
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.
|
Fields Summary |
---|
private final Writer | outThe writer to decorate. |
Constructors Summary |
---|
public FileWriterWithEncoding(String filename, String encoding)Constructs a FileWriterWithEncoding with a file encoding.
this(new File(filename), encoding, false);
| public FileWriterWithEncoding(File file, Charset encoding, boolean append)Constructs a FileWriterWithEncoding with a file encoding.
super();
this.out = initWriter(file, encoding, append);
| public FileWriterWithEncoding(File file, CharsetEncoder encoding)Constructs a FileWriterWithEncoding with a file encoding.
this(file, encoding, false);
| public FileWriterWithEncoding(File file, CharsetEncoder encoding, boolean append)Constructs a FileWriterWithEncoding with a file encoding.
super();
this.out = initWriter(file, encoding, append);
| public FileWriterWithEncoding(String filename, String encoding, boolean append)Constructs a FileWriterWithEncoding with a file encoding.
this(new File(filename), encoding, append);
| public FileWriterWithEncoding(String filename, Charset encoding)Constructs a FileWriterWithEncoding with a file encoding.
this(new File(filename), encoding, false);
| public FileWriterWithEncoding(String filename, Charset encoding, boolean append)Constructs a FileWriterWithEncoding with a file encoding.
this(new File(filename), encoding, append);
| public FileWriterWithEncoding(String filename, CharsetEncoder encoding)Constructs a FileWriterWithEncoding with a file encoding.
this(new File(filename), encoding, false);
| public FileWriterWithEncoding(String filename, CharsetEncoder encoding, boolean append)Constructs a FileWriterWithEncoding with a file encoding.
this(new File(filename), encoding, append);
| public FileWriterWithEncoding(File file, String encoding)Constructs a FileWriterWithEncoding with a file encoding.
this(file, encoding, false);
| public FileWriterWithEncoding(File file, String encoding, boolean append)Constructs a FileWriterWithEncoding with a file encoding.
super();
this.out = initWriter(file, encoding, append);
| public FileWriterWithEncoding(File file, Charset encoding)Constructs a FileWriterWithEncoding with a file encoding.
this(file, encoding, false);
|
Methods Summary |
---|
public void | close()Close the stream.
out.close();
| public void | flush()Flush the stream.
out.flush();
| private static java.io.Writer | initWriter(java.io.File file, java.lang.Object encoding, boolean append)Initialise the wrapped file writer.
Ensure that a cleanup occurs if the writer creation fails.
if (file == null) {
throw new NullPointerException("File is missing");
}
if (encoding == null) {
throw new NullPointerException("Encoding is missing");
}
boolean fileExistedAlready = file.exists();
OutputStream stream = null;
Writer writer = null;
try {
stream = new FileOutputStream(file, append);
if (encoding instanceof Charset) {
writer = new OutputStreamWriter(stream, (Charset)encoding);
} else if (encoding instanceof CharsetEncoder) {
writer = new OutputStreamWriter(stream, (CharsetEncoder)encoding);
} else {
writer = new OutputStreamWriter(stream, (String)encoding);
}
} catch (IOException ex) {
IOUtils.closeQuietly(writer);
IOUtils.closeQuietly(stream);
if (fileExistedAlready == false) {
FileUtils.deleteQuietly(file);
}
throw ex;
} catch (RuntimeException ex) {
IOUtils.closeQuietly(writer);
IOUtils.closeQuietly(stream);
if (fileExistedAlready == false) {
FileUtils.deleteQuietly(file);
}
throw ex;
}
return writer;
| public void | write(int idx)Write a character.
out.write(idx);
| public void | write(char[] chr)Write the characters from an array.
out.write(chr);
| public void | write(char[] chr, int st, int end)Write the specified characters from an array.
out.write(chr, st, end);
| public void | write(java.lang.String str)Write the characters from a string.
out.write(str);
| public void | write(java.lang.String str, int st, int end)Write the specified characters from a string.
out.write(str, st, end);
|
|