JAVA——IO流 之 处理流之打印流:PrintStream与printWriter(3)

来源:互联网 发布:javascript # 编辑:程序博客网 时间:2024/05/18 03:50

续 JAVA——IO流 之 节点流与处理流(2),接着总结处理流——打印流。

1. PrintStream与printWriter介绍

(1)PrintStream 是打印输出流,它继承于FilterOutputStream。
(2)PrintStream 是用来装饰其它输出流。它能为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式。
(3)与其他输出流不同,PrintStream(或printWriter) 永远不会抛出 IOException;它产生的IOException会被自身的函数所捕获并设置错误标记, 用户可以通过 checkError() 返回错误标记,从而查看PrintStream内部是否产生了IOException。
(4)另外,PrintStream 提供了自动flush和字符集设置功能。
(5)printWriter、printStream 都属于输出流,分别针对字符,字节。

   printWriter、printStream 提供了重载的 printprintln 方法:         [1]对于基本数据类型的数据,printprintln方法会先将它们转换成字符串的形式后输出,而不是输出原始的字节内容。         [2]对于一个非基本数据类型的对象,printprintln方法会先调用对象的toSting方法,然后在输出toString方法返回的字符串。    printprintln 能够用于多种数据类型的输出。   printWriter、printStream 的输出操作不会抛出异常,用户通过检测错误状态获取错误信息。   printWriter、printStream 都有自动 flush() 的功能。所谓自动flush,就是往PrintStream写入的数据会立刻调用flush()函数。

(6)print 流具有以下常用的构造方法:
 

    printWriter(Writer out)   printWriter(Writer out, boolean autoFlush)   printWriter(outputStream out)    printWriter(outputStream out,  boolean autoFlush)   printStream(outputStream out)   printStream(outputStream out,  boolean autoFlush)    printStream(outputStream out,  boolean autoFlush,String encoding)

其中,autoFlush控制在Java中遇到换行符(\n)时是否自动清空缓冲区;encoding是指定编码方式。

2. PrintStream与printWriter示例

  • 示例1:
import java.io.*;public class TestPrintStream1 {   public static void main(String[] args) {    PrintStream ps = null;    try {      FileOutputStream fos = new FileOutputStream("d:\\bak\\log.dat");      ps = new PrintStream(fos);    } catch (IOException e) {      e.printStackTrace();    }    if(ps != null){      System.setOut(ps);    }    int ln = 0;    for(char c = 0; c <= 60000; c++){      System.out.print(c+ " ");      if(ln++ >=100){ System.out.println(); ln = 0;}    }  }}
  • 注意:
    (1)System.out是一个PrintStream,System.setOut(ps)重新设置了输出值,即输出到对象ps流中。
    (2)由于修改了输出值,PrintStream又包装了fos,因此System.out.print(c+ ” “);打印输出到了知道的文件log.dat中。
  • 示例2:
import java.util.*; import java.io.*;public class TestPrintStream3 {  public static void main(String[] args) {    String s = null;    BufferedReader br = new BufferedReader(                        new InputStreamReader(System.in));    try {      FileWriter fw = new FileWriter                           ("d:\\bak\\logfile.log", true); //Log4J      PrintWriter log = new PrintWriter(fw);      while ((s = br.readLine())!=null) {        if(s.equalsIgnoreCase("exit")) break;        System.out.println(s.toUpperCase());        log.println("-----");        log.println(s.toUpperCase());         log.flush();      }      log.println("==="+new Date()+"===");       log.flush();      log.close();    } catch (IOException e) {      e.printStackTrace();    }  }}

3. PrintStream和DataOutputStream异同点

  • 【转载】http://www.cnblogs.com/skywang12345/p/io_16.html
  • 相同点:都是继承与FileOutputStream,用于包装其它输出流。
  • 不同点:
    (01) PrintStream和DataOutputStream 都可以将数据格式化输出;但它们在“输出字符串”时的编码不同。PrintStream是输出时采用的是用户指定的编码(创建PrintStream时指定的),若没有指定,则采用系统默认的字符编码。而DataOutputStream则采用的是UTF-8。 关于UTF-8的字符编码可以参考“字符编码(ASCII,Unicode和UTF-8) 和 大小端”。关于DataOutputStream的更多内容,可以参考“java io系列15之 DataOutputStream(数据输出流)的认知、源码和示例”。

    (02) 它们的写入数据时的异常处理机制不同
    DataOutputStream在通过write()向“输出流”中写入数据时,若产生IOException,会抛出。而PrintStream在通过write()向“输出流”中写入数据时,若产生IOException,则会在write()中进行捕获处理;并设置trouble标记(用于表示产生了异常)为true。用户可以通过checkError()返回trouble值,从而检查输出流中是否产生了异常。

    (03) 构造函数不同
    DataOutputStream的构造函数只有一个:DataOutputStream(OutputStream out)。即它只支持以输出流out作为“DataOutputStream的输出流”。而PrintStream的构造函数有许多:和DataOutputStream一样,支持以输出流out作为“PrintStream输出流”的构造函数;还支持以“File对象”或者“String类型的文件名对象”的构造函数。 而且,在PrintStream的构造函数中,能“指定字符集”和“是否支持自动flush()操作”。

    (04) 目的不同
    DataOutputStream的作用是装饰其它的输出流,它和DataInputStream配合使用:允许应用程序以与机器无关的方式从底层输入流中读写java数据类型。而PrintStream的作用虽然也是装饰其他输出流,但是它的目的不是以与机器无关的方式从底层读写java数据类型;而是为其它输出流提供打印各种数据值表示形式,使其它输出流能方便的通过print(), println()或printf()等输出各种格式的数据。

4. 将System.out转换成PrintWriter

  • 原理:System.out是一个PrintStream,而PrintStream是一个OutputStream。PrintWriter有一个可以接收OutputStream作为参数的构造器,因此,只需要将那个构造器的OutputStream换成System.out就可以。
  • 示例:
import java.io.*;public class ChangeSystemOut {    public static void main(String[] args)     {        PrintWriter out = new PrintWriter(System.out,true);        out.println("Hello, world!");        //System.out.println("Hello, World!");    }}
  • 打印输出:
D:\JavaProject\demo13_IO\PrintStream>javac ChangeSystemOut.javaD:\JavaProject\demo13_IO\PrintStream>java ChangeSystemOutHello, world!
  • 注意:要使用有两个参数的PrintWriter的构造器,并将第二个参数设为true,以便开启自动清空的功能;否则,将看不到输出。
构造方法:public PrintWriter(OutputStream out,boolean autoFlush)方法作用:Creates a new PrintWriter from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.参数:out - An output streamautoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer./*autoFlush :控制在Java中遇到换行符\n时是否自动清空缓冲区。其值为true,是;反之,否。