打印流
PrintStream
public void println()自动换行
PrintWriter
package com.itheima.println;import java.io.FileInputStream;
import java.io.PrintStream;
import java.io.PrintWriter;public class Test2 {public static void main(String[] args) {try (//1、创建一个打印流管道PrintWriter ps = new PrintWriter("IO/src/itheima08.txt");){ps.println(97);ps.println("a");ps.print(true);ps.print("我是好人");} catch (Exception e) {e.printStackTrace();}}
}
输出语句的重定向
package com.itheima.println;import java.io.FileNotFoundException;
import java.io.PrintStream;public class Test4 {public static void main(String[] args) {System.out.println("老骥伏枥");System.out.println("志在千里");try (PrintStream ps = new PrintStream("IO/src/itheima09.txt");){//把系统默认的打印流对象改成自己设置的打印流System.setOut(ps);System.out.println("烈士暮年");System.out.println("壮心不已");} catch (Exception e) {e.printStackTrace();}}
}