Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? (http://www.velocityreviews.com/forums/t753243-diff-between-filewriter-f-txt-and-outputstreamwriter-new-fileoutputstream-f-txt.html)

Jochen Brenzlinger 08-26-2011 04:52 AM

Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ?
 
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


Silvio 08-26-2011 07:22 AM

Re: Diff. between FileWriter("f.txt") and OutputStreamWriter(newFileOutputStream("f.txt")) ?
 
On 08/26/2011 06:52 AM, Jochen Brenzlinger wrote:
> 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

Lew 08-26-2011 07:54 AM

Re: Diff. between FileWriter("f.txt") and OutputStreamWriter(newFileOutputStream("f.txt")) ?
 
Jochen Brenzlinger wrote:
> 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.

--
Lew


Roedy Green 08-31-2011 09:13 PM

Re: Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ?
 
On 26 Aug 2011 04:52:14 GMT, jochen2@brenz.com (Jochen Brenzlinger)
wrote, quoted or indirectly quoted someone who said :

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

Arne Vajhøj 09-06-2011 01:17 AM

Re: Diff. between FileWriter("f.txt") and OutputStreamWriter(newFileOutputStream("f.txt")) ?
 
On 8/26/2011 12:52 AM, Jochen Brenzlinger wrote:
> 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

Mayeul 09-06-2011 09:34 AM

Re: Diff. between FileWriter("f.txt") and OutputStreamWriter(newFileOutputStream("f.txt")) ?
 
On 26/08/2011 06:52, Jochen Brenzlinger wrote:
> 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.

--
Mayeul

Roedy Green 09-06-2011 01:53 PM

Re: Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ?
 
On 26 Aug 2011 04:52:14 GMT, jochen2@brenz.com (Jochen Brenzlinger)
wrote, quoted or indirectly quoted someone who said :

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

Roedy Green 09-15-2011 01:23 AM

Re: Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ?
 
On Tue, 06 Sep 2011 11:34:32 +0200, Mayeul <mayeul.marguet@free.fr>
wrote, quoted or indirectly quoted someone who said :

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


All times are GMT. The time now is 10:05 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.