wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes PrintWriter write() and println() method query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "PrintWriter write() and println() method query" Watch "PrintWriter write() and println() method query" New topic
Author

PrintWriter write() and println() method query

Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1071
    
    1

Just realized that PrintWriter has both write() and println() method.
Any idea what is the difference between them and which function to use when ?



Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
Shwetha Sharma
Ranch Hand

Joined: Jun 01, 2008
Posts: 34

PrintWriter.println(String)
This method prints null if the argument passed to it is null.

PrintWriter.write(String)
This method throws a null pointer exception if the argument is null.

Apart from this it works the same as the write method except that it appends "\n" to the string argument.

Also the println method throws IOException while the PrintWriter.write doesn't throw IOException

SCJP6:80%, OCWCD 88 %
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1071
    
    1

Thanks for the reply
Understood
praneeth ram
Ranch Hand

Joined: Sep 21, 2009
Posts: 60
why we can use write() method in the case of AJAX why not println() method.
could you please explain it.

Thanks,
Praneeth.
SCJP,SCWCD.OCP.
 
wood burning stoves
 
subject: PrintWriter write() and println() method query
 
Similar Threads
PrintWriter and OutputStream
println() to a PrintWriter and write() to a ServletOutputStream()
PrintWriter And ServletOutputStream API Doubt
QuizCard txt file - newline missing
difference between PrintWriter.println and PrintWriter.write