Overview and characteristics of the print stream

Overview and characteristics of the print stream

  • 1. What is the print stream
    • This can easily flow the object toString () result output, and automatically add line feed, and can use the auto-refresh mode.

    • System.out is a PrintStream, which is the default output information to the console.

        PrintStream ps = System.out;
        ps.println(97);					//其实底层用的是Integer.toString(x),将x转换为数字字符串打印
        ps.println("xxx");
        ps.println(new Person("张三", 23));
        Person p = null;
        ps.println(p);					//如果是null,就返回null,如果不是null,就调用对象的toString()
      
  • 2. use
    • Print: print (), println ()

    • 自动刷出:PrintWriter(OutputStream out, boolean autoFlush, String encoding)

    • Print object data stream only operation

        PrintWriter pw = new PrintWriter(new FileOutputStream("g.txt"), true);
        pw.write(97);
        pw.print("大家好");
        pw.println("你好");				//自动刷出,只针对的是println方法
        pw.close();
      
package com.heima.otherio;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;

import com.heima.bean.Person;

public class Demo07_PrintStream {

	/**
	 * @param args
	 * @throws IOException 
	 * PrintStream和PrintWriter分别是打印的字节流和字符流
	 * 只操作数据目的的
	 */
	public static void main(String[] args) throws IOException {
		//demo01();
		PrintWriter pw = new PrintWriter(new FileOutputStream("f.txt"),true);
		//pw.println(97);						//自动刷出功能只针对的是println方法
		//pw.write(97);
		pw.print(97);
		pw.println(97);
		pw.close();
	}

	public static void demo01() {
		System.out.println("aaa");
		PrintStream ps = System.out;			//获取标注输出流
		ps.println(97);							//底层通过Integer.toString()将97转换成字符串并打印
		ps.write(97);							//查找码表,找到对应的a并打印
		
		Person p1 = new Person("张三", 23);
		ps.println(p1);							//默认调用p1的toString方法
		
		Person p2 = null;						//打印引用数据类型,如果是null,就打印null,如果不是null就打印对象的toString方法
		ps.println(p2);
		ps.close();
	}
}
Published 295 original articles · won praise 9 · views 60000 +

Guess you like

Origin blog.csdn.net/LeoZuosj/article/details/104115642