17 IO流(十四)——Print流

PrintStream流

PrintStream作为一个包装流,它可以包装字节流,甚至可以使用指定的文件创建一个打印流。
它的构造函数很丰富,建议打开API看一下。

它常用的方法是print方法与println方法,它们与构造方法配合可以不用flush而自动刷新。

构造方法的参数boolean autoFlush不对write方法生效。需要使用flush刷新。

System.out默认输出到控制台,可以使用System.setOut(PrintStream p),此时System.out.print方法就将数据输出到p
System.out可以作为一个输出流

import java.io.*;
public class IOTest01
{
	public static void main(String[] args)throws FileNotFoundException,Exception{
		
		PrintStream ps = System.out;
		ps.println("hello");

		byte[] b = {1,2,3,4};
		PrintStream pps =new PrintStream(
			new BufferedOutputStream(
				new FileOutputStream("abc.txt")),true,"utf-8");
		pps.println("我不开心");
		pps.println("但我会好起来的");
		pps.write(b);
		pps.flush();
		pps.close();
	}
}

  

PrintWriter流

PrintWriter流的构造方法和PrintStream的构造方法高度相似。

不写了。

posted @ 2019-11-26 10:33  Scorpicat  阅读(168)  评论(0编辑  收藏  举报