• 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的构造方法高度相似。

    不写了。

  • 相关阅读:
    glusterfs 术语
    python 随便
    ubuntu glusterfs 配置调试
    源码生成deb包
    常用apt cli
    unexpected error ConnectionError object has no attribute
    [MFC]透明图展示
    菜鸟的mongoDB学习---(六)MongoDB 索引
    Keyboard的显示与隐藏
    HDU 4268 Alice and Bob(贪心+Multiset的应用)
  • 原文地址:https://www.cnblogs.com/Scorpicat/p/11933764.html
Copyright © 2020-2023  润新知