Java/Theory

[Java/Theory]25. I/O에 있는 각 클래스별 차이점 비교분석

양승길 2016. 7. 31. 20:59

[Java/Theory]25. I/O에 있는 각 클래스별 차이점 비교분석

(참고문헌 : Java Application Programming Interface)


BufferedWriter VS PrintWriter

BufferedWriter

Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

The buffer size may be specified, or the default size may be accepted. The default is large enough for most purposes.

A newLine() method is provided, which uses the platform's own notion of line separator as defined by the system property line.separator. Not all platforms use the newline character ('\n') to terminate lines. Calling this method to terminate each output line is therefore preferred to writing a newline character directly.

In general, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters. For example,

 PrintWriter out
   = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
 

will buffer the PrintWriter's output to the file. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.


PrintWriter

Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.

Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the printlnprintf, or format methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character.

Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError().




'Java > Theory' 카테고리의 다른 글

[Java/Theory]28. Executor  (0) 2016.09.17
[Java/Theory]26. 이클립스 관련 클래스패스 문제  (0) 2016.09.05
[Java/Theory]24. I/O Object전송과 Serialization  (0) 2016.07.29
[Java/Theory]23. JDBC  (0) 2016.05.31
[Java/Theory]22. Thread  (0) 2016.05.31