Java-Java I/O流解读之java.io.PrintStream java.io.PrintWriter
- 概述
- 示例
- 代碼
概述
JavaI/O流分為兩類,字節(jié)流和字符流。
字節(jié)流是指InputStream/OutputStream及其子類,
字符流是指Reader/Writer及其子類。
這兩類I/O流的class hierarchy基本上是對(duì)等的,InputStreamReader/OutputStreamWriter是InputStream/OutputStream和Reader/Writer之間的橋梁。
PrintStream是OutputStream的子類,PrintWriter是Writer的子類,兩者處于對(duì)等的位置上.
PrintStream 在 OutputStream 基礎(chǔ)之上提供了增強(qiáng)的功能 , 即可以方便的輸出各種類型的數(shù)據(jù) ( 而不僅限于 byte 類型 ).PrintStrem 的方法從不拋出 IOException.
Printwriter 提供了 PrintStream 的所有打印方法 , 其方法也從不拋出 IOException
與 PrintStream 的區(qū)別 : 作為處理流使用時(shí) ,PrintStream 只能封裝 OutputStream 類型的字節(jié)流 , 而 PrintWriter 既可以封裝 OutputStream, 也能封裝 Writer 類型的字符輸出流并增強(qiáng)其功能 .
一個(gè)字符(char)是16bit,一個(gè)BYTE是8bit. PrintStream是寫入一串8bit的數(shù)據(jù)。
PrintWriter是寫入一串16bit的數(shù)據(jù)。
基于字節(jié)的java.io.printSteam支持方便的打印方法,如print()和println(),用于打印原語和文本字符串。
在JDK 1.5中引入了printf()和format(),用于符格式化輸出。 printf()和format()是一樣的。
PrintStream不會(huì)拋出IOException。相反,它設(shè)置一個(gè)可以通過checkError()方法檢查的內(nèi)部標(biāo)志。還可以創(chuàng)建一個(gè)PrintStream來自動(dòng)刷新輸出。也就是說,flush()方法在寫入一個(gè)字節(jié)數(shù)組之后被自動(dòng)調(diào)用,其中一個(gè)println()方法被調(diào)用,或者在一個(gè)換行符(’\ n’)被寫入之后。
標(biāo)準(zhǔn)輸出和錯(cuò)誤流(System.out和System.err)屬于PrintStream。
PrintStream打印的所有字符都將使用默認(rèn)字符編碼轉(zhuǎn)換為字節(jié)。在需要編寫字符而不是字節(jié)的情況下,應(yīng)使用PrintWriter類。
字符流PrintWriter類似于PrintStream,除了它以字符而不是字節(jié)編寫。 PrintWriter還支持print(),println(),printf()和format()的所有方便的打印方式。它不會(huì)拋出IOException,并且可以選擇創(chuàng)建以支持自動(dòng)刷新。
示例
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 {@Testpublic void test() {// Write data on console using PrintStreamPrintStream pConsol = new PrintStream(System.out);try {pConsol.write("Data to Write on Console using PrintStream".getBytes());} catch (IOException e) {e.printStackTrace();}// flush streampConsol.flush();// close streampConsol.close();// Write data in file using PrintStreamPrintStream 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 streamif (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 {@Testpublic void test() {// Write data on console using PrintWriterPrintWriter pwConsole = new PrintWriter(System.out);pwConsole.write("Data to Write on Console using PrintWriter");pwConsole.flush();pwConsole.close();// Write data in file using PrintWriterPrintWriter 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
總結(jié)
以上是生活随笔為你收集整理的Java-Java I/O流解读之java.io.PrintStream java.io.PrintWriter的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java-Java I/O流解读之基于字
- 下一篇: Java-Java I/O流解读之Obj