PrintWriter write() vs print() method in Java [duplicate]

匿名 (未验证) 提交于 2019-12-03 00:46:02

问题:

This question already has an answer here:

What is the difference between write() and print() methods in PrintWriter class in Java?

回答1:

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

source: http://www.coderanch.com/t/398792/java/java/write-print



回答2:

print() formats the output, while write() just prints the characters it is given. print() handles many argument types

According to coderanch and this one too



回答3:

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.



回答4:

write(int) writes a single character (hence takes the Unicode character of the passed 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.

For more details, you can refer the documentation of PrintWriter.



回答5:

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

print method has higher level of abstraction.

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

check this



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!