Java 中的 PrintWriter write() 与 print() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30662642/
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
PrintWriter write() vs print() method in Java
提问by Sasikumar Murugesan
What is the difference between write()
and print()
methods in PrintWriter
class in Java?
Java中的类write()
和print()
方法有什么区别PrintWriter
?
采纳答案by Maantje
print() formats the output, while write() just prints the characters it is given. print() handles many argument types, converting them into printable strings of characters with String.valueOf(), while write() just handles single characters, arrays of characters, and strings.
To illustrate the difference, write(int) interprets the argument as a single character to be printed, while print(int) converts the integer into a character string. write(49) prints a "1", while print(49) prints "49".
print() 格式化输出,而 write() 只打印给定的字符。print() 处理许多参数类型,使用 String.valueOf() 将它们转换为可打印的字符串,而 write() 只处理单个字符、字符数组和字符串。
为了说明差异,write(int) 将参数解释为要打印的单个字符,而 print(int) 将整数转换为字符串。write(49) 打印“1”,而 print(49) 打印“49”。
source: http://www.coderanch.com/t/398792/java/java/write-print
回答by Coding Enthusiast
print() formats the output, while write() just prints the characters it is given. print() handles many argument types
print() 格式化输出,而 write() 只打印给定的字符。print() 处理许多参数类型
According to coderanchand this one too
回答by Jan
write() is supposed to be used when you need to just print characters while print() is supported to be used when you need to format character output.
当您只需要打印字符时应该使用 write() 而当您需要格式化字符输出时支持使用 print() 。
回答by BhandariS
public void write(String s)
Write a string. This method cannot be inherited from the Writer class because it must suppress I/O exceptions.supports only int and String as parameters
写一个字符串。此方法不能从 Writer 类继承,因为它必须抑制 I/O 异常。仅支持 int 和 String 作为参数
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.supports all primitive data types
打印一个字符串。如果参数为空,则打印字符串“空”。否则,根据平台默认的字符编码将字符串的字符转换为字节,这些字节完全按照 write(int) 方法的方式写入。支持所有原始数据类型
check this
检查这个
回答by Anindya Dutta
write(int)
writes a single character (hence takes the Unicode character of the passed int
).
write(int)
写入单个字符(因此采用传递的 Unicode 字符int
)。
print(datatype)
on the other hand converts the datatype (int
, char
, etc) to a String by first calling String.valueOf(int)
or String.valueOf(char)
which is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int)
method.
print(datatype)
另一方面int
,char
通过首先调用String.valueOf(int)
或将数据类型(,等)转换为字符串,String.valueOf(char)
根据平台的默认字符编码将其转换为字节,并且这些字节完全按照write(int)
方法的方式写入。
For more details, you can refer the documentationof PrintWriter
.
欲了解更多详情,您可以参阅文件中PrintWriter
。