This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between PrintWriter.println and PrintWriter.write  RSS feed

 
yogesh patel
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me what is the difference between the println method and write method of printwriter.

Thanks
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Java Ranch, yogesh. You should familiarize yourself with the Java API Documentation. It has the answers you are looking for:

public void println(String x)
Print a String and then terminate the line.

public void write(String s)
Write a string.

java.io.PrintWriter
 
nilesh kamani
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Please see the below code:-

out.write(request.getParameter("name"));

I got the java.lang.NullPointerException when there is no "name" parameter passed to url.

Tell me why below two sample codes gives the same output for
url .
http://localhost:8080/myapps/FirstServlet?name=kamani&name=bhavin&name=kashyap
Output: nilesh bhavin kashayp
1)
String value[] = request.getParameterValues("name");
for(int i=0; i<value.length; i++)
out.println(value[i] + " ");
2)
String value[] = request.getParameterValues("name");
for(int i=0; i<value.length; i++)
out.write(value[i] + " ");

for out.println output should be
nilesh
bhavin
kashyap

can you please clarify this?

Thanks & Regards
Nilesh Kamani
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi nilesh,

Welcome to JavaRanch!

In the future, when asking a new question, please start your own thread -- don't continue someone else's, especially not one that's over a year old.

But to answer your question: the outputs are not the same. One has newlines between the words, and the other doesn't. But it looks like you're writing a servlet, and that what you're printing ends up being displayed as HTML. In HTML, newlines are not significant: the output of both of these would therefore look the same in a browser. Try doing a "View Source" to see the HTML itself, and you'll see the difference.

If you want to start a new line in HTML, you need to use "<BR>" or "<P>" or some other HTML means to insert a line break.
 
nilesh kamani
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Thanks for your reply.
Can you please answer my first question?
Can you please tell me which method is better to use?
out.write() or out.println()

Thanks
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only evident difference is that println() prints a line separator ('\n') at the end of the output. That been said, behind the scenes, all print methods actually invoke the corresponding write methods.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!