Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ?

J

Jochen Brenzlinger

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
 
S

Silvio

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
 
L

Lew

Jochen said:
As you know there are two ways of writing text into a text file:

Well, there are more than two ways, but that's not relevant to your question, really.
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?

The second one is rather more compact and easy for a human to read. The docs for FileWriter
<http://download.oracle.com/javase/7/docs/api/java/io/FileWriter.html>
suggest when you'd rather use the first one.
 
R

Roedy Green

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?

Trace. Likely you will find there is no real difference, just a
matter of which convenience methods are used to glue the Lego blocks
together.
--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
 
A

Arne Vajhøj

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?

The semantics defined by the API means that the difference will be
two ways to do the same thing.

If you look at the SUN implementation, then it you see that the
two ways are indeed identical:

public class FileWriter extends OutputStreamWriter {
public FileWriter(String fileName) throws IOException {
super(new FileOutputStream(fileName));
}

(and no - it does not override any methods)

Arne
 
M

Mayeul

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?

May I add to other answers, both of them create Writers which will use
the environment's default charset.

If you would like to impose a charset to write with, let's say UTF-8,
solution 1) provides a way for it:

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream("MyFile.txt"), "utf-8"));


While solution 2) does not. It probably should if you ask me, but it
does not.
 
R

Roedy Green

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);

You an use my code-generating Applet
http://mindprod.com/applet/fileio.html

You specify your needs with check boxes and it cranks out what I
consider the optimal code.

You may find playing with it will rapidly give you an idea of how you
can connect the various bits of the Java io lego set.
--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
 
R

Roedy Green

May I add to other answers, both of them create Writers which will use
the environment's default charset.

The Applet at http://mindprod.com/applet/fileio.html will generate you
code to read or write text files, with the default encoding, or with a
selected encoding, using UTF-8, buffered, or unbuffered.
--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top