Java-Java I/O流解读之java.io.PrintStream & java.io.PrintWriter

  • 概述
  • 示例
  • 代码

概述

JavaI/O流分为两类,字节流和字符流。
字节流是指InputStream/OutputStream及其子类,
字符流是指Reader/Writer及其子类。

这两类I/O流的class hierarchy基本上是对等的,InputStreamReader/OutputStreamWriter是InputStream/OutputStream和Reader/Writer之间的桥梁。

PrintStream是OutputStream的子类,PrintWriter是Writer的子类,两者处于对等的位置上.

Java-Java I/O流解读之java.io.PrintStream & java.io.PrintWriter_第1张图片

PrintStream 在 OutputStream 基础之上提供了增强的功能 , 即可以方便的输出各种类型的数据 ( 而不仅限于 byte 类型 ).PrintStrem 的方法从不抛出 IOException.

Printwriter 提供了 PrintStream 的所有打印方法 , 其方法也从不抛出 IOException

与 PrintStream 的区别 : 作为处理流使用时 ,PrintStream 只能封装 OutputStream 类型的字节流 , 而 PrintWriter 既可以封装 OutputStream, 也能封装 Writer 类型的字符输出流并增强其功能 .

一个字符(char)是16bit,一个BYTE是8bit. PrintStream是写入一串8bit的数据。
PrintWriter是写入一串16bit的数据。

基于字节的java.io.printSteam支持方便的打印方法,如print()和println(),用于打印原语和文本字符串。

在JDK 1.5中引入了printf()和format(),用于符格式化输出。 printf()和format()是一样的。

PrintStream不会抛出IOException。相反,它设置一个可以通过checkError()方法检查的内部标志。还可以创建一个PrintStream来自动刷新输出。也就是说,flush()方法在写入一个字节数组之后被自动调用,其中一个println()方法被调用,或者在一个换行符(’\ n’)被写入之后。

标准输出和错误流(System.out和System.err)属于PrintStream。

PrintStream打印的所有字符都将使用默认字符编码转换为字节。在需要编写字符而不是字节的情况下,应使用PrintWriter类。

字符流PrintWriter类似于PrintStream,除了它以字符而不是字节编写。 PrintWriter还支持print(),println(),printf()和format()的所有方便的打印方式。它不会抛出IOException,并且可以选择创建以支持自动刷新。

示例

PrintStreamDemo

package com.xgj.master.java.io.fileDemo.characterStreams;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

import org.junit.Test;

/**
 * 
 * 
 * @ClassName: PrintStreamDemo
 * 
 * @Description: PrintStream is subclass of OutputStream that adds extra
 *               functionality to print different type of data.
 * 
 *               PrintStream never throws IOException.
 * 
 *               PrintStream is automatically flushed when a byte array is
 *               written.
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年9月7日 下午2:21:01
 */
public class PrintStreamDemo {

    @Test
    public void test() {
        // Write data on console using PrintStream
        PrintStream pConsol = new PrintStream(System.out);
        try {
            pConsol.write("Data to Write on Console using PrintStream".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // flush stream
        pConsol.flush();
        // close stream
        pConsol.close();

        // Write data in file using PrintStream
        PrintStream pFile = null;
        try {
            pFile = new PrintStream("D:\\xgj.txt");
            pFile.write("Data to Write in File using PrintStream".getBytes());
            pFile.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // close stream
            if (pFile != null) {
                pFile.close();
            }
        }
    }
}

PrintWriterDemo

package com.xgj.master.java.io.fileDemo.characterStreams;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

import org.junit.Test;

/**
 * 
 * 
 * @ClassName: PrintWriterDemo
 * 
 * @Description: PrintWriter is the implementation of Writer class.
 * 
 *               PrintWriter is used to print formatted representation to
 *               text-output-stream.
 * 
 * 
 *               PrintWriter can also be enabled for automatic flush whenever a
 *               newline character is written.
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年9月7日 下午2:27:13
 */
public class PrintWriterDemo {

    @Test
    public void test() {
        // Write data on console using PrintWriter
        PrintWriter pwConsole = new PrintWriter(System.out);
        pwConsole.write("Data to Write on Console using PrintWriter");
        pwConsole.flush();
        pwConsole.close();

        // Write data in file using PrintWriter
        PrintWriter pwFile = null;
        try {
            pwFile = new PrintWriter(new File("D:/text.txt"));
            pwFile.write("Data to Write in File using PrintWriter");
            pwFile.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            pwFile.close();
        }
    }
}

代码

代码已托管到Github—> https://github.com/yangshangwei/JavaMaster

你可能感兴趣的:(【Java,-Java,Base】,Java手札)