Package org.apache.commons.io.output

# Class FileWriterWithEncoding

java.lang.Object
java.io.Writer
java.io.FilterWriter
org.apache.commons.io.output.ProxyWriter
org.apache.commons.io.output.FileWriterWithEncoding

All Implemented Interfaces:
  `Closeable`, `Flushable`, `Appendable`, `AutoCloseable`

public class FileWriterWithEncoding extends ProxyWriter
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 `Charset`, the `Charset`, or a `CharsetEncoder`. If the default encoding is required then use the `FileWriter` directly, rather than this implementation.

To build an instance, use `FileWriterWithEncoding.Builder`.

Since:
  1.4
See Also:
  - `FileWriterWithEncoding.Builder`

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static class`
  `FileWriterWithEncoding.Builder`
  Builds a new `FileWriterWithEncoding`.

- ## Field Summary

  ### Fields inherited from class java.io.FilterWriter

  `out`

  ### Fields inherited from class java.io.Writer

  `lock`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `FileWriterWithEncoding(File file, String charsetName)`
  Deprecated.
  Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
  `FileWriterWithEncoding(File file, String charsetName, boolean append)`
  Deprecated.
  Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
  `FileWriterWithEncoding(File file, Charset charset)`
  Deprecated.
  Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
  `FileWriterWithEncoding(File file, CharsetEncoder charsetEncoder)`
  Deprecated.
  Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
  `FileWriterWithEncoding(File file, CharsetEncoder charsetEncoder, boolean append)`
  Deprecated.
  Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
  `FileWriterWithEncoding(File file, Charset encoding, boolean append)`
  Deprecated.
  Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
  `FileWriterWithEncoding(String fileName, String charsetName)`
  Deprecated.
  Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
  `FileWriterWithEncoding(String fileName, String charsetName, boolean append)`
  Deprecated.
  Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
  `FileWriterWithEncoding(String fileName, Charset charset)`
  Deprecated.
  Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
  `FileWriterWithEncoding(String fileName, CharsetEncoder encoding)`
  Deprecated.
  Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
  `FileWriterWithEncoding(String fileName, CharsetEncoder charsetEncoder, boolean append)`
  Deprecated.
  Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
  `FileWriterWithEncoding(String fileName, Charset charset, boolean append)`
  Deprecated.
  Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static FileWriterWithEncoding.Builder`
  `builder()`
  Constructs a new `FileWriterWithEncoding.Builder`.

  ### Methods inherited from class org.apache.commons.io.output.ProxyWriter

  `afterWrite, append, append, append, beforeWrite, close, flush, handleIOException, write, write, write, write, write`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### FileWriterWithEncoding

    @Deprecated public FileWriterWithEncoding(File file, Charset charset) throws IOException
    Deprecated.
    Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
    Constructs a FileWriterWithEncoding with a file encoding.
    Parameters:
      `file` - the file to write to, not null
      `charset` - the encoding to use, not null
    Throws:
      `NullPointerException` - if the file or encoding is null
      `IOException` - in case of an I/O error

  - ### FileWriterWithEncoding

    @Deprecated public FileWriterWithEncoding(File file, Charset encoding, boolean append) throws IOException
    Deprecated.
    Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
    Constructs a FileWriterWithEncoding with a file encoding.
    Parameters:
      `file` - the file to write to, not null.
      `encoding` - the name of the requested charset, null uses the default Charset.
      `append` - true if content should be appended, false to overwrite.
    Throws:
      `NullPointerException` - if the file is null.
      `IOException` - in case of an I/O error.

  - ### FileWriterWithEncoding

    @Deprecated public FileWriterWithEncoding(File file, CharsetEncoder charsetEncoder) throws IOException
    Deprecated.
    Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
    Constructs a FileWriterWithEncoding with a file encoding.
    Parameters:
      `file` - the file to write to, not null
      `charsetEncoder` - the encoding to use, not null
    Throws:
      `NullPointerException` - if the file or encoding is null
      `IOException` - in case of an I/O error

  - ### FileWriterWithEncoding

    @Deprecated public FileWriterWithEncoding(File file, CharsetEncoder charsetEncoder, boolean append) throws IOException
    Deprecated.
    Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
    Constructs a FileWriterWithEncoding with a file encoding.
    Parameters:
      `file` - the file to write to, not null.
      `charsetEncoder` - the encoding to use, null uses the default Charset.
      `append` - true if content should be appended, false to overwrite.
    Throws:
      `NullPointerException` - if the file is null.
      `IOException` - in case of an I/O error.

  - ### FileWriterWithEncoding

    @Deprecated public FileWriterWithEncoding(File file, String charsetName) throws IOException
    Deprecated.
    Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
    Constructs a FileWriterWithEncoding with a file encoding.
    Parameters:
      `file` - the file to write to, not null
      `charsetName` - the name of the requested charset, not null
    Throws:
      `NullPointerException` - if the file or encoding is null
      `IOException` - in case of an I/O error

  - ### FileWriterWithEncoding

    @Deprecated public FileWriterWithEncoding(File file, String charsetName, boolean append) throws IOException
    Deprecated.
    Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
    Constructs a FileWriterWithEncoding with a file encoding.
    Parameters:
      `file` - the file to write to, not null.
      `charsetName` - the name of the requested charset, null uses the default Charset.
      `append` - true if content should be appended, false to overwrite.
    Throws:
      `NullPointerException` - if the file is null.
      `IOException` - in case of an I/O error.

  - ### FileWriterWithEncoding

    @Deprecated public FileWriterWithEncoding(String fileName, Charset charset) throws IOException
    Deprecated.
    Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
    Constructs a FileWriterWithEncoding with a file encoding.
    Parameters:
      `fileName` - the name of the file to write to, not null
      `charset` - the charset to use, not null
    Throws:
      `NullPointerException` - if the file name or encoding is null
      `IOException` - in case of an I/O error

  - ### FileWriterWithEncoding

    @Deprecated public FileWriterWithEncoding(String fileName, Charset charset, boolean append) throws IOException
    Deprecated.
    Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
    Constructs a FileWriterWithEncoding with a file encoding.
    Parameters:
      `fileName` - the name of the file to write to, not null
      `charset` - the encoding to use, not null
      `append` - true if content should be appended, false to overwrite
    Throws:
      `NullPointerException` - if the file name or encoding is null
      `IOException` - in case of an I/O error

  - ### FileWriterWithEncoding

    @Deprecated public FileWriterWithEncoding(String fileName, CharsetEncoder encoding) throws IOException
    Deprecated.
    Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
    Constructs a FileWriterWithEncoding with a file encoding.
    Parameters:
      `fileName` - the name of the file to write to, not null
      `encoding` - the encoding to use, not null
    Throws:
      `NullPointerException` - if the file name or encoding is null
      `IOException` - in case of an I/O error

  - ### FileWriterWithEncoding

    @Deprecated public FileWriterWithEncoding(String fileName, CharsetEncoder charsetEncoder, boolean append) throws IOException
    Deprecated.
    Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
    Constructs a FileWriterWithEncoding with a file encoding.
    Parameters:
      `fileName` - the name of the file to write to, not null
      `charsetEncoder` - the encoding to use, not null
      `append` - true if content should be appended, false to overwrite
    Throws:
      `NullPointerException` - if the file name or encoding is null
      `IOException` - in case of an I/O error

  - ### FileWriterWithEncoding

    @Deprecated public FileWriterWithEncoding(String fileName, String charsetName) throws IOException
    Deprecated.
    Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
    Constructs a FileWriterWithEncoding with a file encoding.
    Parameters:
      `fileName` - the name of the file to write to, not null
      `charsetName` - the name of the requested charset, not null
    Throws:
      `NullPointerException` - if the file name or encoding is null
      `IOException` - in case of an I/O error

  - ### FileWriterWithEncoding

    @Deprecated public FileWriterWithEncoding(String fileName, String charsetName, boolean append) throws IOException
    Deprecated.
    Use `builder()`, `FileWriterWithEncoding.Builder`, and `FileWriterWithEncoding.Builder.get()`
    Constructs a FileWriterWithEncoding with a file encoding.
    Parameters:
      `fileName` - the name of the file to write to, not null
      `charsetName` - the name of the requested charset, not null
      `append` - true if content should be appended, false to overwrite
    Throws:
      `NullPointerException` - if the file name or encoding is null
      `IOException` - in case of an I/O error

- ## Method Details

  - ### builder

    public static FileWriterWithEncoding.Builder builder()
    Constructs a new `FileWriterWithEncoding.Builder`.
    Returns:
      Creates a new `FileWriterWithEncoding.Builder`.
    Since:
      2.12.0