Difference between java.io.PrintWriter and java.io

2019-01-10 05:17发布

Please look through code below:

// A.class
File file = new File("blah.txt");
FileWriter fileWriter = new FileWriter(file);
PrintWriter printWriter = new PrintWriter(fileWriter);

// B.class
File file = new File("blah.txt");
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bWriter = new BufferedWriter(fileWriter);

What is the difference between these two methods?

When should we use PrintWriter over BufferedWriter?

8条回答
戒情不戒烟
2楼-- · 2019-01-10 06:04

BufferedWriter - Writes text to an output character stream, buffering characters from a character stream. PrintWriter - Prints formatted representations of objects to a text output stream.

查看更多
看我几分像从前
3楼-- · 2019-01-10 06:11

PrintWriter just exposes the print methods on any Writer in character mode.

BufferedWriter is more efficient than , according to its buffered methods. And it comes with a newLine() method, depending of your system platform, to manipulate text files correctly.

The BufferedReader permits to read a text from file, with bytes converted in characters. It allows to read line by line.

There is no PrintReader, you have to choose another Reader implementation according to the format of your input.

查看更多
登录 后发表回答