2

Hello,

I want to know the difference between println() and write() methods used in java servlet.

out.println("Hello");
out.write("Hello");

How the above code will be stored?? Why can we use both the methods for the writing the same text as above..

hmjd
  • 120,187
  • 20
  • 207
  • 252
user460920
  • 887
  • 5
  • 17
  • 27
  • 2
    Can you modify your question so that you explicitly give the type of out? It will help others reading your question. – kittylyst Feb 14 '12 at 10:00
  • possible duplicate of [What is the exact difference between out.write() and out.print()](http://stackoverflow.com/questions/18338134/what-is-the-exact-difference-between-out-write-and-out-print) – Raedwald Sep 16 '14 at 10:12

3 Answers3

5

The out variable in your case is most likely refers to a PrintWriter, so the answer to your question lies in the API documentation of that class.

Just compare the description of 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.

... with the description of println ...

public void println(String x)

Print a String and then terminate the line. This method behaves as though it invokes print(String) and then println().

... and 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.

All in all I'd say that the print methods work on a higher level of abstraction and is the one I prefer to work with when writing servlets.

aioobe
  • 413,195
  • 112
  • 811
  • 826
0

The only plausible difference with PrintWriter.println has to with flushing which is invoked in case of using println but not print("\n") in all other respects it would behave same as print()

Plus the PrintWriter class also doent throw any Exception.

manocha_ak
  • 904
  • 7
  • 19
-4

1.out.write("") method is for java.io.Writer it is used to write the any file like text,csv.

2.out.println("Hello") this one is servlet method and it is use for write data on browser.

Suresh
  • 1,494
  • 3
  • 13
  • 14
  • 1
    Are you mad. Have you ever seen the API of java.lang.System class. – Chandra Sekhar Feb 14 '12 at 10:10
  • Actually i understand the question as different.I understand he is asking about java write method and servlet method thats why i wrote this. – Suresh Feb 14 '12 at 10:14
  • it's nothing to with servlet as such. – manocha_ak Feb 14 '12 at 10:15
  • Ya i got it now.But you can see the questionar tag the java and servlet so you tell what people understand.you know this also used in servlet. – Suresh Feb 14 '12 at 10:17
  • Thanks everyone..for the response – user460920 Feb 14 '12 at 10:23
  • I am newbie of servlet java programming. So,i am confused in some books it is mentioned with the out object the println() method and in others i found with the out object is write() method. So,when to use which method i am little bit confused..So,with context to servlet i am asking when we can use write method and when to use println() methods??? – user460920 Feb 14 '12 at 10:36
  • 1
    I hope u got the difference between 2 what i want to say.http://www.jsptutorial.net/writing-the-first-java-servlet.aspx that expalaing out.println and other one just they used method in java http://onjava.com/pub/a/onjava/excerpt/jebp_3/index1.html?page=2 – Suresh Feb 14 '12 at 10:42