PrintWriter Question

Discussion in 'Java' started by Danger_Duck, Aug 18, 2008.

  1. Danger_Duck

    Danger_Duck Guest

    Continuing my vein of simple java questions...For the
    Java.io.PrintWriter, what is the difference between write and print?

    For me, print and write mean the same thing so the difference between:

    print(char[] s)
    Print an array of characters.

    and

    write(char[] buf)
    Write an array of characters.

    is unknown. This is made especially acute when the documentation says
    things like:
    "print

    public void print(char c)

    Print a character. The character is translated into one or more
    bytes according to the platform's default character encoding, and
    these bytes are written in exactly the manner of the write(int)
    method."

    Anyone care to enlighten me on the difference and recommend when to
    use which method?
    Thanks!
    Danger_Duck, Aug 18, 2008
    #1
    1. Advertising

  2. Danger_Duck wrote:
    > Continuing my vein of simple java questions...For the
    > Java.io.PrintWriter, what is the difference between write and print?


    The write methods are overridden from java.io.Writer. The print methods
    are defined in java.io.PrintWriter, most likely retained for consistence
    with java.io.PrintStream.

    So write(char[]) == print(char[]), but the latter should probably be
    preferred.


    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Aug 18, 2008
    #2
    1. Advertising

  3. Danger_Duck <> wrote:
    > public void print(char c)
    >
    > Print a character. The character is translated into one or more
    > bytes according to the platform's default character encoding, and
    > these bytes are written in exactly the manner of the write(int)
    > method."


    int i=42;
    PrintWriter pw= ...;
    pw.print(i); // --> "42"
    pw.write(i); // --> "*" (ascii 42)

    It's like they differ by some ".toString()" conversion.
    For "char" and "String"(unless null), there's no visible
    difference. "print" is generally more comfortable to use.

    You can lookup the base-class'es docu for the write-methods.
    The PrintWriter's versions of them are just wrappers to hide
    IOExceptions.
    Andreas Leitgeb, Aug 18, 2008
    #3
  4. Danger_Duck

    Mark Space Guest

    Danger_Duck wrote:
    > Continuing my vein of simple java questions...For the
    > Java.io.PrintWriter, what is the difference between write and print?


    class PrintTest {
    public static void main( String ... args ) {
    char [] c = { 42 };
    PrintWriter pw = new PrintWriter( new OutputStreamWriter(
    System.out ) );
    pw.print( c );
    pw.write( c );
    pw.flush();
    }
    }

    compile:
    run:
    **
    BUILD SUCCESSFUL (total time: 0 seconds)

    Both print '*', so I dunno. Possibly whoever extended the Writer class
    to make the PrintWriter class felt there should be methods named "print"
    not "write" so they just added them, even if there's no functional
    difference.
    Mark Space, Aug 18, 2008
    #4
  5. Mark Space wrote:
    > Danger_Duck wrote:
    >> Continuing my vein of simple java questions...For the
    >> Java.io.PrintWriter, what is the difference between write and print?

    >
    > class PrintTest {
    > public static void main( String ... args ) {
    > char [] c = { 42 };
    > PrintWriter pw = new PrintWriter( new OutputStreamWriter(
    > System.out ) );
    > pw.print( c );
    > pw.write( c );
    > pw.flush();
    > }
    > }
    >
    > compile:
    > run:
    > **
    > BUILD SUCCESSFUL (total time: 0 seconds)
    >
    > Both print '*', so I dunno. Possibly whoever extended the Writer
    > class to make the PrintWriter class felt there should be methods
    > named "print" not "write" so they just added them, even if there's no
    > functional difference.


    The print() methods are not declared as throwing IOExceptions, which the
    write() methods are. In fact, neither of them actually does (an IOException
    sets an error flag which can be checked with checkError(), but then returns
    normally. This makes the print() methods more convenient to use when
    IOExceptions needn't be handled immediately, e.g. when writing to stdout.
    Mike Schilling, Aug 19, 2008
    #5
  6. Mike Schilling <> wrote:
    > Mark Space wrote:
    >> Both print '*', so I dunno.
    >> ... so they just added them, even if there's no
    >> functional difference.


    > .... This makes the print() methods more convenient to use when
    > IOExceptions needn't be handled immediately, e.g. when writing to stdout.


    There *is* a functional difference between write and print, except
    for the char, char[] and String overloads, which appear to be the
    same except for null-values.

    Try to call each with an "int" argument.
    Andreas Leitgeb, Aug 19, 2008
    #6
    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. Oz Levanon
    Replies:
    1
    Views:
    2,087
  2. Houman
    Replies:
    2
    Views:
    696
    Frank
    Aug 18, 2004
  3. Sharp
    Replies:
    5
    Views:
    392
    Paul Lutus
    Oct 15, 2004
  4. Lars Willich
    Replies:
    0
    Views:
    579
    Lars Willich
    Jan 28, 2005
  5. CamT
    Replies:
    1
    Views:
    4,389
    Oscar kind
    Mar 16, 2005
Loading...

Share This Page