Java out.write() 和 out.print() 之间的确切区别是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18338134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:40:53  来源:igfitidea点击:

What is the exact difference between out.write() and out.print()

javaservletsprintwriter

提问by Kumaran Palani

In my servlet I gave both out.printand out.write. but both prints in the browser.

在我的 servlet 中,我同时提供了out.printout.write. 但都在浏览器中打印。

What is the exact difference between these two and when to use out.printand out.write?

这两者之间的确切区别是什么以及何时使用out.printout.write

回答by JNL

PrintWriter:

打印写入器

public void write(String s)

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

公共无效写(字符串)

写一个字符串。此方法不能从 Writer 类继承,因为它必须抑制 I/O 异常。

print method has higher level of abstraction.

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.

公共无效打印(字符串)

打印一个字符串。如果参数为空,则打印字符串“空”。否则,根据平台的默认字符编码将字符串的字符转换为字节,这些字节完全按照 write(int) 方法的方式写入。

Hope this helps.

希望这可以帮助。

回答by Ashish Chaurasia

The out variable in your case is most likely refers to a PrintWriter

您的情况下的 out 变量很可能是指 PrintWriter

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.

public void write(String s)

写一个字符串。此方法不能从 Writer 类继承,因为它必须抑制 I/O 异常。

... with the description of println ...

...带有 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().

public void println(String x)

打印一个字符串,然后终止该行。此方法的行为就像它先调用 print(String) 然后调用 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.

public void print(String s)

打印一个字符串。如果参数为空,则打印字符串“空”。否则,根据平台的默认字符编码将字符串的字符转换为字节,这些字节完全按照 write(int) 方法的方式写入。

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.

总而言之,我会说打印方法在更高的抽象级别上工作,并且是我在编写 servlet 时更喜欢使用的方法。

回答by Bohemian

The basic difference is that out.write()explodes if you pass it a null:

基本的区别是,out.write()如果你传递一个空值,它会爆炸:

String s = null;
out.print(s); // outputs the text "null"
out.write(s); // NullPointerException

回答by Pravin Kumar

out.write(-) vs out.print(-)

out.write(-) 与 out.print(-)

One more difference is out.write(-)method just write data or object to browser like a file. You can not write any statement like out.write(10*20);but we do this with out.print(10*20);

另一个区别是 out.write(-)方法只是将数据或对象像文件一样写入浏览器。你不能写像out.write(10*20);这样的语句但我们用out.print(10*20);

回答by DHARMRAJSINGH

write() method only writes characters to stream(or console) but does not print, while print() method writes and print it on stream (or console).

write() 方法只将字符写入流(或控制台)但不打印,而 print() 方法写入并在流(或控制台)上打印。

System.out.write(97);
System.out.print('j');

first statement writes character 97 i.e 'a' on console but does not print, while second statement prints 'a' which is written already on stream and 'j' which is passed in print() method.

第一条语句在控制台上写入字符 97,即 'a' 但不打印,而第二条语句打印已写入流的 'a' 和在 print() 方法中传递的 'j'。

回答by user2418306

PrintWriter's implementation communicates the difference better than javadoc

PrintWriter的实现比 javadoc 更好地传达了差异

public void print(String s) {
    if (s == null) {
        s = "null";
    }
    write(s);
}

回答by user2418306

There are three major differences:

主要有以下三个区别:

1) If you try to print a null value of a String with out.write(), It will throw NullPointerExceptionwhile out.print()will simply print NULLas a string.

1) 如果您尝试使用out.write()打印字符串的空值,它将抛出NullPointerExceptionout.print()将简单地将NULL打印为字符串。

 String name = null;
 out.write(name); // NullPointerException
 out.print(name); // 'Null' as text will be printed

2) out.print()can print Boolean values but out.write()can not.

2) out.print()可以打印布尔值但out.write()不能。

boolean b = true;
out.write(b); // Compilation error
out.print(b); // 'true' will be printed 

3) If you are using out.write(), you simply can not place arithmetic operation code but out.print()provides the support.

3) 如果您使用的是out.write(),您根本无法放置算术运算代码,但是out.print()提供了支持。

out.write(10+20); // No output will be displayed.
out.print(10+20); // Output '30' will be displayed. 

回答by Poorna Senani Gamage

I simply know it as like this:

我只是知道它是这样的:

out.println()is method of javax.?servlet.?jsp.?JspWriter

out.println()是方法 javax.?servlet.?jsp.?JspWriter

out.write()is method of java.io.Writer

out.write()是方法 java.io.Writer

回答by Roshana Pitigala

First thing is you can't use javax.?servlet.?jsp.?JspWriter outin a servlet. It has to be used in a .jspfile, because outis a method local variable in _jspService(...)method of your .jspfile.

第一件事是你不能javax.?servlet.?jsp.?JspWriter out在 servlet 中使用。它在使用.jsp的文件,因为out是在一个方法局部变量_jspService(...)的方法.jsp文件。

There is no difference in the purpose of using out.print()and out.write(). Both are used to write the Stringversion of the given object to JspWriter's buffer.

out.print()和的使用目的没有区别out.write()两者都用于String将给定对象的版本写入 JspWriter 的缓冲区。

However, JspWriter.print()is capable of taking many types of arguments than Writer.write().

但是,JspWriter.print()能够采用多种类型的参数,而不是Writer.write().

JspWriter.print()

JspWriter.print()

  • Object
  • String
  • boolean
  • char
  • char[]
  • double
  • float
  • int
  • long
  • 目的
  • 细绳
  • 布尔值
  • 字符
  • 字符[]
  • 双倍的
  • 漂浮
  • 整数

Writer.write()

Writer.write()

  • String
  • char
  • int
  • 细绳
  • 字符
  • 整数