概述:
在整个IO包中,打印流是输出信息做方便的类,主要包含字节打印流(PrintStream)和字符打印流(PrintWriter)。打印流提供了非常方便的打印功能,可以打印任何的数据类型,例如:小数、整数、字符串、布尔类型等等。
一、PrintStream类
为什么要有PrintStream类?
之前在打印信息的时候需要使用OutputStream,但是这样一来,所有的数据输出的时候会非常的麻烦,String——>byte[],但是字节打印流中可以方便的进行输出。
PrintStream类的优点
PrintStream在OutputStream基础之上提供了增强的功能,可以方便的输出各种类型的数据(不仅限于byte型)的格式化表示形式。
1.1 构造方法
在PrintStream中定义的构造方法中可以清楚的发现有一个构造方法可以直接接收OutputStream类的实例,这是因为与OutputStream相比起来,PrintStream可以更加方便的输出数据,这就好比将OutputStream重新包装了一下,使之输出更加方便。
1.2 常用方法
该方法被重载很多次,可以输出任意数据。
public void print(Type x);
该方法被重载很多次,可以输出任意数据后换行。
public void println(Type x);
1.3 把数据写入到文件中
【示例】把指定的数据,写入到文件中
public class PrintStreamDemo { public static void main(String[] args) throws FileNotFoundException { // 创建一个字符打印流对象 PrintStream ps = new PrintStream("demo.txt"); // 把数组中的元素值输入到文件中 Object[] arr = { true, 123, "powernode", 123.45}; for(int i = 0; i < arr.length; i++) { // 打印数据 ps.println(arr[i]); } ps.close(); } } |
1.4 格式化输出
在JDK1.5之后,JAVA又对PrintStream类进行了扩充,增加了格式化的输出方式,直接使用printf()方法就可以完成操作,但是在进行格式化输出的时候需要指定其输出的数据类型。
字符 |
描述 |
%s |
表示内容为字符串 |
%d |
表示内容为整数 |
%f |
表示内容为小数 |
%c |
表示内容为字符 |
【示例】格式化输出案例
public class PrintStreamDemo { public static void main(String[] args) throws FileNotFoundException { // 创建一个字符打印流对象 PrintStream ps = new PrintStream("demo.txt"); // 格式化输出 ps.printf("姓名:%s 年龄:%d 成绩:%f 性别:%c","小明", 8, 97.5, '男'); ps.close(); // System.out的格式化输出 System.out.printf("姓名:%s 年龄:%d 成绩:%f 性别:%c", "小明", 18, 97.5, '男'); } } |
二、PrintWriter类
概述:
PrintWriter提供了PrintStream的所有打印方法,其方法也从不抛出IOException。
PrintWriter与PrintStream的区别:
作为处理流使用时,PrintStream只能封装OutputStream类型的字节流,而PrintWriter既可以封装OutputStream类型的字节流,还能够封装Writer类型的字符输出流并增强其功能。
1.1 构造方法
注意:设置了自动刷新,那么只有在换行的时候才会提现效果!
1.2 把数据写入到文件中
【示例】把指定的数据,写入到文件中
public class PrintWriterDemo { public static void main(String[] args) throws IOException { FileWriter writer = new FileWriter("demo.txt"); // 创建字符打印流对象,并设置为自定刷新 PrintWriter pw = new PrintWriter(writer, true); // 存储数据 for(int i = 0; i < 10; i++) { pw.println("hello world"); } pw.close(); } } |
补充:字符打印流也支持数据的格式化输出。
三、标准输入&输出流
概述:
Java通过系统类System实现标准输入&输出的功能,定义了3个流变量:in、out和err。这三个流在Java中都定义为静态变量,可以直接通过System类进行调用。
1)System.in表示标准输入,通常指从键盘输入数据;
2)System.out表示标准输出,通常指把数据输出到控制台或者屏幕;
3)System.err表示标准错误输出,通常指把数据输出到控制台或者屏幕。
1.1 简单标准输入(System.in)
System.in作为字节输入流类InputStream的对象实现标准输入,通过read()方法从键盘接受数据。
【示例】获取控制台输入的数据
public class InputStreamDemo { public static void main(String[] args) throws IOException { // 获取一个标准的输入流 InputStream in = System.in; // 读取键盘输入的内容 byte[] by = new byte[1024]; int count = in.read(by); // 遍历输出读取到的内容 for(int i = 0; i < count; i++) { System.out.println("-->" + by[i]); } // 注意:通过System.in获取到的标准输入流不用关闭! } } |
运行程序,从键盘输入3个字符abc并按Enter键,保存在缓冲区bf中的元素个数count为5,Enter占用最后两个字节('\r'和'\n'),下图为控制台的效果。
在通常情况下需要从标准输入读取字符,整数,浮点数等具体类型的数据System.in作为标准输入流,是InputStream类的一个对象,其read()方法的主要功能是读取字节和字节数组,不能直接得到需要的数据(如整型,浮点型)。此时,需要另外一个类java.util.Scanner的配合。可以利用Scanner类对标准输入流System.in的数据进行解析,得到需要的数据。
1.2 简单标准输出(System.out)
System.out作为打印流PrintStream的对象实现标准输出,其定义了print和println方法,支持将Java的任意基本类型作为参数。
【示例】在控制台输输出的数据
public class PrintStreamTest { public static void main(String[] args) { // 获取一个标准的输出流 PrintStream ps = System.out; // 在控制台输出数据 ps.println(123); ps.println(123.456); ps.println("hello world"); ps.println(true); ps.println('A'); ps.println(new Date()); // 注意:通过System.out获取到的标准输出流不用关闭! } } |
注意:通过System.in和System.out获取到的标准输入&输出流建议不要关闭!