Home

FileWriter

FileWriter is a class in the Java standard library that provides a character stream for writing to files. It is part of the java.io package and is designed to simplify writing text data to a file by handling the underlying byte stream conversion automatically. FileWriter is generally used when writing character data, such as text, rather than binary data.

FileWriter is typically constructed with a file name or a File object, and may optionally enable append

When you write through a FileWriter, the data is converted from characters to bytes using the platform’s

FileWriter inherits from Writer and supports typical writer operations such as write(char[]), write(String), flush, and close.

Limitations include the reliance on the platform default encoding and that FileWriter is intended for text

mode.
Example
constructors
include
new
FileWriter(String
fileName),
new
FileWriter(File
file),
and
new
FileWriter(String
fileName,
boolean
append).
Many
of
these
constructors
throw
IOException
if
the
file
cannot
be
created
or
opened.
default
charset,
unless
an
alternative
approach
is
used.
For
explicit
character
encoding,
developers
often
wrap
a
FileOutputStream
in
an
OutputStreamWriter
with
a
specified
Charset,
or
use
higher-level
APIs
such
as
Files.newBufferedWriter(Path,
Charset).
For
improved
performance
on
larger
writes,
it
is
common
to
wrap
a
FileWriter
with
a
BufferedWriter,
which
minimizes
the
number
of
I/O
operations.
data.
For
binary
data,
other
streams
such
as
FileOutputStream
are
more
appropriate.