java.io.PrintWriter 和 java.io.BufferedWriter 的区别? [英] Difference between java.io.PrintWriter and java.io.BufferedWriter?

查看:33
本文介绍了java.io.PrintWriter 和 java.io.BufferedWriter 的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面的代码:

// 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?

我们什么时候应该使用 PrintWriter 而不是 BufferedWriter?

When should we use PrintWriter over BufferedWriter?

推荐答案

BufferedWriterPrintWriter 详细说明差异.

The API reference for BufferedWriter and PrintWriter detail the differences.

使用 PrintWriter 的主要原因是可以访问 printXXX 方法,例如 println().您基本上可以使用 PrintWriter 写入文件,就像使用 System.out 写入控制台一样.

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.

BufferedWriter 是一种写入文件(或其他任何东西)的有效方法,因为它会在(可能,取决于实现)丢弃到 C 来写入文件之前将字符缓冲在 Java 内存中.

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.

没有PrintReader"这样的概念;最接近的可能是 java.util.Scanner.

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

这篇关于java.io.PrintWriter 和 java.io.BufferedWriter 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆