FileDocCategorySizeDatePackage
FileWriterWithEncoding.javaAPI DocAndroid 1.5 API12394Wed May 06 22:42:46 BST 2009org.apache.commons.io.output

FileWriterWithEncoding

public 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.

since
Commons IO 1.4
version
$Id: FileWriterWithEncoding.java 611634 2008-01-13 20:35:00Z niallp $

Fields Summary
private final Writer
out
The writer to decorate.
Constructors Summary
public FileWriterWithEncoding(String filename, String encoding)
Constructs a FileWriterWithEncoding with a file encoding.

param
filename the name of the file to write to, not null
param
encoding the encoding to use, not null
throws
NullPointerException if the file name or encoding is null
throws
IOException in case of an I/O error

        this(new File(filename), encoding, false);
    
public FileWriterWithEncoding(File file, Charset encoding, boolean append)
Constructs a FileWriterWithEncoding with a file encoding.

param
file the file to write to, not null
param
encoding the encoding to use, not null
param
append true if content should be appended, false to overwrite
throws
NullPointerException if the file or encoding is null
throws
IOException in case of an I/O error

        super();
        this.out = initWriter(file, encoding, append);
    
public FileWriterWithEncoding(File file, CharsetEncoder encoding)
Constructs a FileWriterWithEncoding with a file encoding.

param
file the file to write to, not null
param
encoding the encoding to use, not null
throws
NullPointerException if the file or encoding is null
throws
IOException in case of an I/O error

        this(file, encoding, false);
    
public FileWriterWithEncoding(File file, CharsetEncoder encoding, boolean append)
Constructs a FileWriterWithEncoding with a file encoding.

param
file the file to write to, not null
param
encoding the encoding to use, not null
param
append true if content should be appended, false to overwrite
throws
NullPointerException if the file or encoding is null
throws
IOException in case of an I/O error

        super();
        this.out = initWriter(file, encoding, append);
    
public FileWriterWithEncoding(String filename, String encoding, boolean append)
Constructs a FileWriterWithEncoding with a file encoding.

param
filename the name of the file to write to, not null
param
encoding the encoding to use, not null
param
append true if content should be appended, false to overwrite
throws
NullPointerException if the file name or encoding is null
throws
IOException in case of an I/O error

        this(new File(filename), encoding, append);
    
public FileWriterWithEncoding(String filename, Charset encoding)
Constructs a FileWriterWithEncoding with a file encoding.

param
filename the name of the file to write to, not null
param
encoding the encoding to use, not null
throws
NullPointerException if the file name or encoding is null
throws
IOException in case of an I/O error

        this(new File(filename), encoding, false);
    
public FileWriterWithEncoding(String filename, Charset encoding, boolean append)
Constructs a FileWriterWithEncoding with a file encoding.

param
filename the name of the file to write to, not null
param
encoding the encoding to use, not null
param
append true if content should be appended, false to overwrite
throws
NullPointerException if the file name or encoding is null
throws
IOException in case of an I/O error

        this(new File(filename), encoding, append);
    
public FileWriterWithEncoding(String filename, CharsetEncoder encoding)
Constructs a FileWriterWithEncoding with a file encoding.

param
filename the name of the file to write to, not null
param
encoding the encoding to use, not null
throws
NullPointerException if the file name or encoding is null
throws
IOException in case of an I/O error

        this(new File(filename), encoding, false);
    
public FileWriterWithEncoding(String filename, CharsetEncoder encoding, boolean append)
Constructs a FileWriterWithEncoding with a file encoding.

param
filename the name of the file to write to, not null
param
encoding the encoding to use, not null
param
append true if content should be appended, false to overwrite
throws
NullPointerException if the file name or encoding is null
throws
IOException in case of an I/O error

        this(new File(filename), encoding, append);
    
public FileWriterWithEncoding(File file, String encoding)
Constructs a FileWriterWithEncoding with a file encoding.

param
file the file to write to, not null
param
encoding the encoding to use, not null
throws
NullPointerException if the file or encoding is null
throws
IOException in case of an I/O error

        this(file, encoding, false);
    
public FileWriterWithEncoding(File file, String encoding, boolean append)
Constructs a FileWriterWithEncoding with a file encoding.

param
file the file to write to, not null
param
encoding the encoding to use, not null
param
append true if content should be appended, false to overwrite
throws
NullPointerException if the file or encoding is null
throws
IOException in case of an I/O error

        super();
        this.out = initWriter(file, encoding, append);
    
public FileWriterWithEncoding(File file, Charset encoding)
Constructs a FileWriterWithEncoding with a file encoding.

param
file the file to write to, not null
param
encoding the encoding to use, not null
throws
NullPointerException if the file or encoding is null
throws
IOException in case of an I/O error

        this(file, encoding, false);
    
Methods Summary
public voidclose()
Close the stream.

throws
IOException if an I/O error occurs

        out.close();
    
public voidflush()
Flush the stream.

throws
IOException if an I/O error occurs

        out.flush();
    
private static java.io.WriterinitWriter(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.

param
file the file to be accessed
param
encoding the encoding to use - may be Charset, CharsetEncoder or String
param
append true to append
return
the initialised writer
throws
NullPointerException if the file or encoding is null
throws
IOException if an error occurs

        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 voidwrite(int idx)
Write a character.

param
idx the character to write
throws
IOException if an I/O error occurs

        out.write(idx);
    
public voidwrite(char[] chr)
Write the characters from an array.

param
chr the characters to write
throws
IOException if an I/O error occurs

        out.write(chr);
    
public voidwrite(char[] chr, int st, int end)
Write the specified characters from an array.

param
chr the characters to write
param
st The start offset
param
end The number of characters to write
throws
IOException if an I/O error occurs

        out.write(chr, st, end);
    
public voidwrite(java.lang.String str)
Write the characters from a string.

param
str the string to write
throws
IOException if an I/O error occurs

        out.write(str);
    
public voidwrite(java.lang.String str, int st, int end)
Write the specified characters from a string.

param
str the string to write
param
st The start offset
param
end The number of characters to write
throws
IOException if an I/O error occurs

        out.write(str, st, end);