Print打印流顾名思义,只有输出没有输入
1、PrintWriter和PrintStream都属于输出流,分别针对与字符和字节
2、PrintWriter和PrintStream提供了重载的print
3、PrintWriter和PrintStream有自动flush功能
4、PrintWriter和PrintStream的输出操作不会抛出异常,用户通过检测错误状态获取错误信息
5、Println方法用于多种数据类型的输出
一、Print与System.out.println区别
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class Test8 {
public static void main(String[] args) throws FileNotFoundException {
PrintStream ps=new PrintStream(new FileOutputStream("D:/log.txt"));
//创建新的文件D:/log.txt字节输出流,并将其保存到PrintStream对象中
ps.print("HelloWorld");
System.out.println("1");
//打印在屏幕上
System.setOut(ps);
//设置打印在PrintStream对象中
System.out.println("2");
//打印在文件中D:/log.txt
try {
FileOutputStream file=new FileOutputStream("A:sasa.sas");
} catch (Exception e) {
e.printStackTrace(ps);
//将异常打印在文件中
}
ps.close();
}
}