Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: PrintWriter: passing File reference vs String

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Thanked 0 Times in 0 Posts

    Default PrintWriter: passing File reference vs String

    What is the difference between passing a PrintWriter a reference to a File object and passing it the String filename? If you just want to use the format method to write to a file, both arguments seem to do the same thing, but my instructor always passes a File object instead of just a String. Is that way better somehow?

  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Thanked 206 Times in 182 Posts

    Default Re: PrintWriter: passing File reference vs String

    To see the differences between the various constructors, the place to start is their API documentation.

    It is a fair bet that the library writers will not include multiple constructors for any class unless there *is* some reasonably common occasion when you would prefer one to another. (This applies to all eight constructors, not just the two you mention - each of them might be preferable depending on circumstances.) Your choice will not lie in the fact want to use the printf() methods: after all, these methods do the same thing however the PrintWriter instance is constructed. One reason why a person might prefer the File constructor over the String one is if they intend using the File reference for other purposes like testing whether the file exists and is writable.

Similar Threads

  1. [SOLVED] Printwriter class: displaying File contents to screen with included formatting?
    By mwebb in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: November 25th, 2011, 11:38 AM
  2. [SOLVED] Difficulty with string passing...
    By Destined in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2011, 05:43 AM
  3. HELP with sending data within text file to client with printwriter
    By dannyyy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2011, 02:42 PM
  4. Passing reference via object
    By Stefan_Lam in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2011, 11:57 AM
  5. passing string into method
    By tabutcher in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 08:43 AM