11
try{

    File file = new File("write.txt");
    FileWriter writer = new FileWriter(file);

    PrintWriter printWriter = new PrintWriter(writer);
    printWriter.println("pqr");
    printWriter.println("jkl");
    printWriter.close();

    PrintWriter printWriter = new PrintWriter(file);
    printWriter.println("abc");
    printWriter.println("xyz");
    printWriter.close();
}

I don't understand what is difference bet'n these two way. In which scenario i should use printWriter and fileWriter.

Rahul
  • 15,979
  • 4
  • 42
  • 63
nakul
  • 1,445
  • 7
  • 20
  • 30
  • Please look for the question already answered on stackoverflow......... http://stackoverflow.com/questions/5759925/printwriter-vs-filewriter-in-java – Prateek Dec 28 '12 at 10:25
  • That question is different than the one asked here. – Manu Jan 16 '20 at 14:21

5 Answers5

10

Although both of these internally uses a FileOutputStream , the main difference is that PrintWriter offers some additional methods for formatting like println and printf.

code snippets:

public PrintWriter(File file) throws FileNotFoundException {
     this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
     false);
}


public FileWriter(File file) throws IOException {
       super(new FileOutputStream(file));
}

Major Differences :

  1. FileWriter throws IOException in case of any IO failure.
  2. None of the PrintWriter methods throws IOException, instead they set a boolean flag which can be obtained using checkError().
  3. PrintWriter comes with an option of autoflush while creation(default is without autoflush) which will flush after every byte of data is written. In case of FileWriter, caller has to take care of invoking flush.
Rahul
  • 15,979
  • 4
  • 42
  • 63
  • 2
    This is wrong: `PrintWriter automatically invokes flush after every byte of data is written`. You may create an object with auto flush, but the default is without auto flush. [See here](http://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html) – Math Oct 30 '14 at 17:35
  • 1
    @Math i think you are confusing PrintWriter class with PrintStream class because in PrintWriter class if automatic flushing is requested at time of object creation then it will be done **only when** one of the println(), printf(), format() methods is invoked and not always. – sactiw Aug 12 '16 at 20:27
  • 1
    @Math's point is absolutely right. PrintWriter **can** be instantiated with autoflush, but the default constructor sets it to false (see Javadoc / source code). So the third point of the answer is misleading IMO; it should read "PrintWriter can be instantiated to automatically flush...". – mrod Apr 19 '18 at 07:31
  • thanks for your inputs. corrected my answer as per your comments. – Rahul Apr 20 '18 at 06:15
  • You write `which will flush after every byte of data is writte`, but is it true? Isn't autoflush enacted when `println()` and similar methods are invoked? Then it's not necessarily a *byte* of data: `pw.println("Hello")` would contrain several bytes that would be flushed at once. – parsecer May 10 '19 at 22:56
  • …and what is the difference between PrintWriter(File) and PrintWriter(FileWriter(File)), as in the question’s excerpt? – Manu Jan 16 '20 at 14:23
2

From the source what PrintWriter does when you pass a File is to open it in a buffered way

public PrintWriter(File file) throws FileNotFoundException {
    this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
         false);
}

if you pass it a FileWriter, it will open it, without buffering

public FileWriter(File file) throws IOException {
    super(new FileOutputStream(file));
}

This means that the first example can be slightly more efficient. However I would use the File without the FileWriter because to me it is simpler.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

PrintWriter gives you some handy methods for formatting like println and printf. So if you need to write printed text - you can use it. FileWriter is more like "low-level" writer that gives you ability to write only strings and char arrays. Basically I don't think there is a big difference what you choose.

Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
0

While FileWriter has only a basic set of methods, PrintWriter has a rich set of convinience methods, one of them is in your example - PrintWriter.println.

You should also keep in mind that "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()"

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

One of the most important differences between the two, is that with FileWriter you can append text to the end of an existing file, whereas with PrintWriter you cannot.