Difference between java.io.PrintWriter and java.io.BufferedWriter?

i2ijeya :

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?

TofuBeer :

The API reference for BufferedWriter and PrintWriter detail the differences.

The main reason to use the PrintWriter is to get access to the printXXX methods like println(). You can essentially use a PrintWriter to write to a file just like you would use System.out to write to the console.

A BufferedWriter is an efficient way to write to a file (or anything else), as it will buffer the characters in Java memory before (probably, depending on the implementation) dropping to C to do the writing to the file.

There is no such concept as a "PrintReader"; the closest you will get is probably java.util.Scanner.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java difference between FileWriter and BufferedWriter

Java: Difference between PrintStream and PrintWriter

Java Client/Server - Using BufferedWriter instead of PrintWriter

What is the difference between append and write methods of java.io.writer?

java.io.PrintWriter +print(cArray: char[] ): void

Java IO difference in reading streams

IO Performance between Go and Java

Java - the difference between BufferedWriter.write() and System.out.println()?

In Java, what is the difference between using a BufferedWriter or writing straight to file?

Difference between java.nio.file.Files and java.io.File?

Java IO vs NIO, what is really difference?

What's the difference between '_io' and 'io'?

Difference between buffered io and unbuffered io

java.io.BufferedWriter throttles or completely stopped during the process of writing, anyone know why?

What is the difference between the method writeChar and writeShort in java.io.DataOutputStream class?

Java io library: What is the difference between File.toString() and File.getPath()

Java IO: Using scanner and printWriter to copy the contents of a text file and put them in another text file

Difference between Kotlin arrow IO, IO.fx, IO !effect

printWriter in Java

My PrintWriter can't output my message giving me this error: Cannot invoke "java.io.PrintWriter.println(String)" because "this.output" is null

Handling IO exceptions in Java

Skip() method in IO java?

Serializing in java file io

Asynchronous IO in Java?

java io FileNotFoundException

Console IO for Java applet

File IO using java

Java concurrent console IO

Java IO Copying File