打印流详解

编程入门 行业动态 更新时间:2024-10-28 14:31:07

打印流<a href=https://www.elefans.com/category/jswz/34/1770044.html style=详解"/>

打印流详解

概述 

作用:打印流可以实现方便、高效的打印数据到文件中去。

高效体现在用到了缓冲流:

    public PrintStream(OutputStream out, boolean autoFlush, Charset charset) {super(out);this.autoFlush = autoFlush;this.charOut = new OutputStreamWriter(this, charset);this.textOut = new BufferedWriter(charOut);}

打印流一般是指:PrintStream,PrintWriter两个类。

可以实现打印什么数据就是什么数据,例如打印整数97写出去就是97,打印boolean的true,写出去就是true。 

PrintStream

 支持写字节数据。

PrintWriter

 支持写字符数据。

打印流示例

/**目标:学会使用打印流 高效  方便写数据到文件。*/
public class PrintDemo1 {public static void main(String[] args) throws Exception {// 1、创建一个打印流对象
//        PrintStream ps = new PrintStream(new FileOutputStream("io-app2/src/ps.txt"));
//        PrintStream ps = new PrintStream(new FileOutputStream("io-app2/src/ps.txt" , true)); // 追加数据,在低级管道后面加True
//        PrintStream ps = new PrintStream("io-app2/src/ps.txt" );PrintWriter ps = new PrintWriter("io-app2/src/ps.txt"); // 打印功能上与PrintStream的使用没有区别ps.println(97);ps.println('a');ps.println(23.3);ps.println(true);ps.println("我是打印流输出的,我是啥就打印啥");ps.close();}
}

输出语句的重定向

/**目标:了解改变输出语句的位置到文件*/
public class PrintDemo2 {public static void main(String[] args) throws Exception {System.out.println("锦瑟无端五十弦");System.out.println("一弦一柱思华年");// 改变输出语句的位置(重定向)PrintStream ps = new PrintStream("io-app2/src/log.txt");System.setOut(ps); // 把系统打印流改成我们自己的打印流System.out.println("庄生晓梦迷蝴蝶");System.out.println("望帝春心托杜鹃");}
}

 

更多推荐

打印流详解

本文发布于:2023-11-15 14:18:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1601103.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:详解

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!