As you know there are two ways of writing text into a text file:
1.) BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("MyFile.txt")));
out.write(s);
and
2.) BufferedWriter out = new BufferedWriter(new FileWriter("MyFile.txt"));
out.write(s);
Whats the difference?
Jochen
In java.io you either do byte IO or character IO. Byte IO can be used
for any form of data without any interpretation of what that data
represents. Character IO is meant for, well, information that is
represented as sequences of characters.
For byte IO we have various types of InputStream/OutputStream classes
like the FileOutputStream from your example 1). For character data whe
have various forms of Reader/Writer classes like the FileWriter from
your example. Buffering can be done in both cases, hence the
BufferedWriter and BufferedOutputStream classes.
To do character IO characters will at some point have to be converted to
byte data using some form of encoding. FileWriter does that for you,
which is why it has a constructor that takes the encoding along with the
file name. Common encodings include UTF-8, UTF-16 and US-ASCII.
OutputStreamWriter is a class that only does the encoding of characters.
That is why it is a Writer and its constructor takes an OutputStream as
parameter with an optional encoding parameter.
If you omit the explicit encoding specifications both FileWriter and
ByteArrayOutputStream will use the default encoding for your platform.
This is something to be very careful with since it will vary among
different JVMs.
A FileWriter could be seen as an OutputStreamWriter wrapped around a
FileOutputStream. That explains why your examples are quite similar.
Gr. Silvio