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

后端 未结 8 1449
南笙
南笙 2020-12-02 06:04

Please look through code below:

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


        
8条回答
  •  爱一瞬间的悲伤
    2020-12-02 06:55

    PrintWriter is the most enhanced Writer to write Character data to a file.

    The main advantage of PrintWriter over FileWriter and BufferedWriter is:

    1. PrintWriter can communicate directly with the file, and can communicate via some Writer object also.

    PrintWriter printwriter = new PrintWriter("blah.txt");

    (or)

    FileWriter filewriter = new FileWriter("blah.txt");
    PrintWriter printwiter = new PrintWriter(filewriter);
    
    1. We can write any type of Primitive data directly to the file (because we have additional overloaded versions of PrintWriter methods i.e., print() and println()).

      printwriter.print(65); //65
      bufferedwriter.write(65); //A
      printwriter.println(true); //true

提交回复
热议问题