Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

print() Vs write() in PrintWriter

 
Tridib Samanta
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am confused about the print() and write() methods available in PrintWriter class. What is the difference between them and what should I use when?
 
Anupama Ponnapalli
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The print methods seem to be wrappers around the write methods.

print

public void print(String s)

Print a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Parameters:
s - The String to be printed

write

public void write(String s)

Write a string. This method cannot be inherited from the Writer class because it must suppress I/O exceptions.

Overrides:
write in class Writer

Parameters:
s - String to be written

The print - println methods are the most commonly used ones with servlets. Most IO classses have write methods, maybe it comes from there.
 
Tridib Samanta
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks much Anupama!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic