PrintWriter流提供了打印方法,可以将各种数据类型的数据都原样打印。
分类
字节打印流(PrintStream):
构造函数可以接收的参数类型。
1.File对象。
2.字符串路径,String。
3.字节输出流,OutputStream。
字符打印流(PrintWriter):
构造函数可以接收的参数类型。
1.File对象。
2.字符串路径,String。
3.字节输出流,OutputStream。
4.字符输出流,Writer。
PrintWriter(OutputStream out, boolean autoFlush):通过现有的 OutputStream 创建新的 PrintWriter。
PrintWriter(Writer out, boolean autoFlush):创建新 PrintWriter。
参数:
out - 输出流
autoFlush - boolean 变量;如果为 true,则 println、printf 或 format 方法将刷新输出缓冲区
示例
import java.io.*;
class PrintStreamDemo{
public static void main(String[] args)throws IOException{
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new FileWriter("a.txt"),true); // 输出到文本文件 Writer
//PrintWriter out = new PrintWriter(System.out,true); // 输出到控制台 OutputStream
String line=null;
while((line=bufr.readLine())!=null){
if("over".equals(line))
break;
out.println(line.toUpperCase());
// out.flush(); // 构造方法已指定了将刷新输出缓冲区
}
out.close();
bufr.close();
}
}