打印流,打印数据的,打印流只能是输出流
PrintStream:字节打印流.
PrintWriter:字符打印流.
- -对于PrintWriter来说,当启用自动刷新之后.
PrintWriter ps = new PrintWriter(new FileOutputStream(new File("file/abc.txt")),true);
PrinterStream,即使不close也会刷新 ,PrintWriter需要调用println或者printf或者format方法.便会立马刷新操作.或者close.
如果没有开启自动刷新,则需要手动刷新或者当缓冲区满的时候,再自动刷新,使用打印流作为输出流,此时的输出操作会特别简单.
因为在打印流中:
提供了print方法:打印不换行
提供了println方法:打印再换行
print和println方法可以支持打印/输出各种数据类型,记住void println(Object obj)既可.
示例代码
public static void main(String[] args) throws Exception {
test1();
test2();
}
//字符打印流
private static void test1() throws Exception {
// TODO Auto-generated method stub
PrintWriter writer = new PrintWriter(new File("target/abc.txt"));
writer.print("no ");
writer.print("pain ");
writer.print("in ");
writer.print("the ");
writer.print("world ");
writer.print("comes ");
writer.print("near ");
writer.print("to ");
writer.print("this!");
writer.close();
}
//字节打印流
private static void test2() throws Exception {
// TODO Auto-generated method stub
PrintStream p = new PrintStream(new File("target/aaa.txt"));
p.print("no ");
p.print("pain ");
p.print("in ");
p.print("the ");
p.print("world ");
p.print("comes ");
p.print("near ");
p.print("to ");
p.print("this!");
//p.close();
}