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

Discussion in 'Java' started by Jochen Brenzlinger, Aug 26, 2011.

  1. 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
    Jochen Brenzlinger, Aug 26, 2011
    #1
    1. Advertising

  2. Jochen Brenzlinger

    Silvio Guest

    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
    Silvio, Aug 26, 2011
    #2
    1. Advertising

  3. Jochen Brenzlinger

    Lew Guest

    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
    Lew, Aug 26, 2011
    #3
  4. Jochen Brenzlinger

    Roedy Green Guest

    On 26 Aug 2011 04:52:14 GMT, (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)
    Roedy Green, Aug 31, 2011
    #4
  5. Jochen Brenzlinger

    Arne Vajhøj Guest

    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
    Arne Vajhøj, Sep 6, 2011
    #5
  6. Jochen Brenzlinger

    Mayeul Guest

    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
    Mayeul, Sep 6, 2011
    #6
  7. Jochen Brenzlinger

    Roedy Green Guest

    On 26 Aug 2011 04:52:14 GMT, (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, Sep 6, 2011
    #7
  8. Jochen Brenzlinger

    Roedy Green Guest

    On Tue, 06 Sep 2011 11:34:32 +0200, Mayeul <>
    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)
    Roedy Green, Sep 15, 2011
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lars Willich
    Replies:
    0
    Views:
    597
    Lars Willich
    Jan 28, 2005
  2. CamT
    Replies:
    1
    Views:
    4,412
    Oscar kind
    Mar 16, 2005
  3. mattias

    new FileWriter hangs

    mattias, Jul 31, 2005, in forum: Java
    Replies:
    5
    Views:
    1,119
    Thomas Hawtin
    Aug 1, 2005
  4. Roedy Green
    Replies:
    6
    Views:
    1,699
    Chris Uppal
    Jan 5, 2006
  5. Santa
    Replies:
    1
    Views:
    1,076
    Mark A. Odell
    Jul 17, 2003
Loading...

Share This Page