PrintStream(打印輸出流)詳解
PrintStream 介紹
PrintStream 是打印輸出流,它繼承於FilterOutputStream。
PrintStream 是用來裝飾其它輸出流。它能為其他輸出流添加了功能,使它們能夠方便地打印各種數據值表示形式。
與其他輸出流不同,PrintStream 永遠不會拋出 IOException;它產生的IOException會被自身的函數所捕獲並設置錯誤標記, 用戶可以通過 checkError() 返回錯誤標記,從而查看PrintStream內部是否產生了IOException。
另外,PrintStream 提供了自動flush 和 字符集設置功能。所謂自動flush,就是往PrintStream寫入的數據會立刻調用flush()函數。
PrintStream 函數列表
/*
* 構造函數
*/
// 將“輸出流out”作為PrintStream的輸出流,不會自動flush,並且采用默認字符集
// 所謂“自動flush”,就是每次執行print(), println(), write()函數,都會調用flush()函數;
// 而“不自動flush”,則需要我們手動調用flush()接口。
PrintStream(OutputStream out)
// 將“輸出流out”作為PrintStream的輸出流,自動flush,並且采用默認字符集。
PrintStream(OutputStream out, boolean autoFlush)
// 將“輸出流out”作為PrintStream的輸出流,自動flush,采用charsetName字符集。
PrintStream(OutputStream out, boolean autoFlush, String charsetName)
// 創建file對應的FileOutputStream,然後將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用默認字符集。
PrintStream(File file)
// 創建file對應的FileOutputStream,然後將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用charsetName字符集。
PrintStream(File file, String charsetName)
// 創建fileName對應的FileOutputStream,然後將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用默認字符集。
PrintStream(String fileName)
// 創建fileName對應的FileOutputStream,然後將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用charsetName字符集。
PrintStream(String fileName, String charsetName)
// 將“字符c”追加到“PrintStream輸出流中”
PrintStream append(char c)
// 將“字符序列從start(包括)到end(不包括)的全部字符”追加到“PrintStream輸出流中”
PrintStream append(CharSequence charSequence, int start, int end)
// 將“字符序列的全部字符”追加到“PrintStream輸出流中”
PrintStream append(CharSequence charSequence)
// flush“PrintStream輸出流緩沖中的數據”,並檢查錯誤
boolean checkError()
// 關閉“PrintStream輸出流”
synchronized void close()
// flush“PrintStream輸出流緩沖中的數據”。
// 例如,PrintStream裝飾的是FileOutputStream,則調用flush時會將數據寫入到文件中
synchronized void flush()
// 根據“Locale值(區域屬性)”來格式化數據
PrintStream format(Locale l, String format, Object... args)
// 根據“默認的Locale值(區域屬性)”來格式化數據
PrintStream format(String format, Object... args)
// 將“float數據f對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數
void print(float f)
// 將“double數據d對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數
void print(double d)
// 將“字符串數據str”寫入到“PrintStream輸出流”中,print實際調用的是write函數
synchronized void print(String str)
// 將“對象o對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數
void print(Object o)
// 將“字符c對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數
void print(char c)
// 將“字符數組chars對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數
void print(char[] chars)
// 將“long型數據l對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數
void print(long l)
// 將“int數據i對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數
void print(int i)
// 將“boolean數據b對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數
void print(boolean b)
// 將“數據args”根據“Locale值(區域屬性)”按照format格式化,並寫入到“PrintStream輸出流”中
PrintStream printf(Locale l, String format, Object... args)
// 將“數據args”根據“默認Locale值(區域屬性)”按照format格式化,並寫入到“PrintStream輸出流”中
PrintStream printf(String format, Object... args)
// 將“換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數
void println()
// 將“float數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數
void println(float f)
// 將“int數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數
void println(int i)
// 將“long數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數
void println(long l)
// 將“對象o對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數
void println(Object o)
// 將“字符數組chars對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數
void println(char[] chars)
// 將“字符串str+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數
synchronized void println(String str)
// 將“字符c對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數
void println(char c)
// 將“double數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數
void println(double d)
// 將“boolean數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數
void println(boolean b)
// 將數據oneByte寫入到“PrintStream輸出流”中。oneByte雖然是int類型,但實際只會寫入一個字節
synchronized void write(int oneByte)
// 將“buffer中從offset開始的length個字節”寫入到“PrintStream輸出流”中。
void write(byte[] buffer, int offset, int length)
注意:print()和println()都是將其中參數轉換成字符串之後,再寫入到輸入流。
例如,
print(0x61);
等價於
write(String.valueOf(0x61));
上面語句是將字符串"97"寫入到輸出流。0x61對應十進制數是97。
write(0x61)
上面語句是將字符'a'寫入到輸出流。因為0x61對應的ASCII碼的字母'a'。
查看下面的代碼,我們能對這些函數有更清晰的認識!
PrintStream 源碼分析(基於jdk1.7.40)
package java.io;
import java.util.Formatter;
import java.util.Locale;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
public class PrintStream extends FilterOutputStream
implements Appendable, Closeable
{
// 自動flush
// 所謂“自動flush”,就是每次執行print(), println(), write()函數,都會調用flush()函數;
// 而“不自動flush”,則需要我們手動調用flush()接口。
private final boolean autoFlush;
// PrintStream是否右產生異常。當PrintStream有異常產生時,會被本身捕獲,並設置trouble為true
private boolean trouble = false;
// 用於格式化的對象
private Formatter formatter;
// BufferedWriter對象,用於實現“PrintStream支持字符集”。
// 因為PrintStream是OutputStream的子類,所以它本身不支持字符串;
// 但是BufferedWriter支持字符集,因此可以通過OutputStreamWriter創建PrintStream對應的BufferedWriter對象,從而支持字符集。
private BufferedWriter textOut;
private OutputStreamWriter charOut;
private static <T> T requireNonNull(T obj, String message) {
if (obj == null)
throw new NullPointerException(message);
return obj;
}
// 返回csn對應的字符集對象
private static Charset toCharset(String csn)
throws UnsupportedEncodingException
{
requireNonNull(csn, "charsetName");
try {
return Charset.forName(csn);
} catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
// UnsupportedEncodingException should be thrown
throw new UnsupportedEncodingException(csn);
}
}
// 將“輸出流out”作為PrintStream的輸出流,autoFlush的flush模式,並且采用默認字符集。
private PrintStream(boolean autoFlush, OutputStream out) {
super(out);
this.autoFlush = autoFlush;
this.charOut = new OutputStreamWriter(this);
this.textOut = new BufferedWriter(charOut);
}
// 將“輸出流out”作為PrintStream的輸出流,自動flush,采用charsetName字符集。
private PrintStream(boolean autoFlush, OutputStream out, Charset charset) {
super(out);
this.autoFlush = autoFlush;
this.charOut = new OutputStreamWriter(this, charset);
this.textOut = new BufferedWriter(charOut);
}
// 將“輸出流out”作為PrintStream的輸出流,自動flush,采用charsetName字符集。
private PrintStream(boolean autoFlush, Charset charset, OutputStream out)
throws UnsupportedEncodingException
{
this(autoFlush, out, charset);
}
// 將“輸出流out”作為PrintStream的輸出流,不會自動flush,並且采用默認字符集
public PrintStream(OutputStream out) {
this(out, false);
}
// 將“輸出流out”作為PrintStream的輸出流,自動flush,並且采用默認字符集。
public PrintStream(OutputStream out, boolean autoFlush) {
this(autoFlush, requireNonNull(out, "Null output stream"));
}
// 將“輸出流out”作為PrintStream的輸出流,自動flush,采用charsetName字符集。
public PrintStream(OutputStream out, boolean autoFlush, String encoding)
throws UnsupportedEncodingException
{
this(autoFlush,
requireNonNull(out, "Null output stream"),
toCharset(encoding));
}
// 創建fileName對應的FileOutputStream,然後將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用默認字符集。
public PrintStream(String fileName) throws FileNotFoundException {
this(false, new FileOutputStream(fileName));
}
// 創建fileName對應的FileOutputStream,然後將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用charsetName字符集。
public PrintStream(String fileName, String csn)
throws FileNotFoundException, UnsupportedEncodingException
{
// ensure charset is checked before the file is opened
this(false, toCharset(csn), new FileOutputStream(fileName));
}
// 創建file對應的FileOutputStream,然後將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用默認字符集。
public PrintStream(File file) throws FileNotFoundException {
this(false, new FileOutputStream(file));
}
// 創建file對應的FileOutputStream,然後將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用csn字符集。
public PrintStream(File file, String csn)
throws FileNotFoundException, UnsupportedEncodingException
{
// ensure charset is checked before the file is opened
this(false, toCharset(csn), new FileOutputStream(file));
}
private void ensureOpen() throws IOException {
if (out == null)
throw new IOException("Stream closed");
}
// flush“PrintStream輸出流緩沖中的數據”。
// 例如,PrintStream裝飾的是FileOutputStream,則調用flush時會將數據寫入到文件中
public void flush() {
synchronized (this) {
try {
ensureOpen();
out.flush();
}
catch (IOException x) {
trouble = true;
}
}
}
private boolean closing = false; /* To avoid recursive closing */
// 關閉PrintStream
public void close() {
synchronized (this) {
if (! closing) {
closing = true;
try {
textOut.close();
out.close();
}
catch (IOException x) {
trouble = true;
}
textOut = null;
charOut = null;
out = null;
}
}
}
// flush“PrintStream輸出流緩沖中的數據”,並檢查錯誤
public boolean checkError() {
if (out != null)
flush();
if (out instanceof java.io.PrintStream) {
PrintStream ps = (PrintStream) out;
return ps.checkError();
}
return trouble;
}
protected void setError() {
trouble = true;
}
protected void clearError() {
trouble = false;
}
// 將數據b寫入到“PrintStream輸出流”中。b雖然是int類型,但實際只會寫入一個字節
public void write(int b) {
try {
synchronized (this) {
ensureOpen();
out.write(b);
if ((b == '\n') && autoFlush)
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
// 將“buf中從off開始的length個字節”寫入到“PrintStream輸出流”中。
public void write(byte buf[], int off, int len) {
try {
synchronized (this) {
ensureOpen();
out.write(buf, off, len);
if (autoFlush)
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
// 將“buf中的全部數據”寫入到“PrintStream輸出流”中。
private void write(char buf[]) {
try {
synchronized (this) {
ensureOpen();
textOut.write(buf);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush) {
for (int i = 0; i < buf.length; i++)
if (buf[i] == '\n')
out.flush();
}
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
// 將“字符串s”寫入到“PrintStream輸出流”中。
private void write(String s) {
try {
synchronized (this) {
ensureOpen();
textOut.write(s);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush && (s.indexOf('\n') >= 0))
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
// 將“換行符”寫入到“PrintStream輸出流”中。
private void newLine() {
try {
synchronized (this) {
ensureOpen();
textOut.newLine();
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush)
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
// 將“boolean數據對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數
public void print(boolean b) {
write(b ? "true" : "false");
}
// 將“字符c對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數
public void print(char c) {
write(String.valueOf(c));
}
// 將“int數據i對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數
public void print(int i) {
write(String.valueOf(i));
}
// 將“long型數據l對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數
public void print(long l) {
write(String.valueOf(l));
}
// 將“float數據f對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數
public void print(float f) {
write(String.valueOf(f));
}
// 將“double數據d對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數
public void print(double d) {
write(String.valueOf(d));
}
// 將“字符數組s”寫入到“PrintStream輸出流”中,print實際調用的是write函數
public void print(char s[]) {
write(s);
}
// 將“字符串數據s”寫入到“PrintStream輸出流”中,print實際調用的是write函數
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
// 將“對象obj對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數
public void print(Object obj) {
write(String.valueOf(obj));
}
// 將“換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數
public void println() {
newLine();
}
// 將“boolean數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數
public void println(boolean x) {
synchronized (this) {
print(x);
newLine();
}
}
// 將“字符x對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數
public void println(char x) {
synchronized (this) {
print(x);
newLine();
}
}
// 將“int數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數
public void println(int x) {
synchronized (this) {
print(x);
newLine();
}
}
// 將“long數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數
// 查看本欄目
說明:
PrintStream的源碼比較簡單,請上文的注釋進行閱讀。若有不明白的地方,建議先看看後面的PrintStream使用示例;待搞清它的作用和用法之後,再來閱讀源碼。
PrintStream和DataOutputStream異同點
相同點:都是繼承與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()等輸出各種格式的數據。
示例代碼
關於PrintStream中API的詳細用法,參考示例代碼(PrintStreamTest.java):
import java.io.PrintStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* PrintStream 的示例程序
*
* @author skywang
*/
public class PrintStreamTest {
public static void main(String[] args) {
// 下面3個函數的作用都是一樣:都是將字母“abcde”寫入到文件“file.txt”中。
// 任選一個執行即可!
testPrintStreamConstrutor1() ;
//testPrintStreamConstrutor2() ;
//testPrintStreamConstrutor3() ;
// 測試write(), print(), println(), printf()等接口。
testPrintStreamAPIS() ;
}
/**
* PrintStream(OutputStream out) 的測試函數
*
* 函數的作用,就是將字母“abcde”寫入到文件“file.txt”中
*/
private static void testPrintStreamConstrutor1() {
// 0x61對應ASCII碼的字母'a',0x62對應ASCII碼的字母'b', ...
final byte[] arr={0x61, 0x62, 0x63, 0x64, 0x65 }; // abced
try {
// 創建文件“file.txt”的File對象
File file = new File("file.txt");
// 創建文件對應FileOutputStream
PrintStream out = new PrintStream(
new FileOutputStream(file));
// 將“字節數組arr”全部寫入到輸出流中
out.write(arr);
// 關閉輸出流
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* PrintStream(File file) 的測試函數
*
* 函數的作用,就是將字母“abcde”寫入到文件“file.txt”中
*/
private static void testPrintStreamConstrutor2() {
final byte[] arr={0x61, 0x62, 0x63, 0x64, 0x65 };
try {
File file = new File("file.txt");
PrintStream out = new PrintStream(file);
out.write(arr);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* PrintStream(String fileName) 的測試函數
*
* 函數的作用,就是將字母“abcde”寫入到文件“file.txt”中
*/
private static void testPrintStreamConstrutor3() {
final byte[] arr={0x61, 0x62, 0x63, 0x64, 0x65 };
try {
PrintStream out = new PrintStream("file.txt");
out.write(arr);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 測試write(), print(), println(), printf()等接口。
*/
private static void testPrintStreamAPIS() {
// 0x61對應ASCII碼的字母'a',0x62對應ASCII碼的字母'b', ...
final byte[] arr={0x61, 0x62, 0x63, 0x64, 0x65 }; // abced
try {
// 創建文件對應FileOutputStream
PrintStream out = new PrintStream("other.txt");
// 將字符串“hello PrintStream”+回車符,寫入到輸出流中
out.println("hello PrintStream");
// 將0x41寫入到輸出流中
// 0x41對應ASCII碼的字母'A',也就是寫入字符'A'
out.write(0x41);
// 將字符串"65"寫入到輸出流中。
// out.print(0x41); 等價於 out.write(String.valueOf(0x41));
out.print(0x41);
// 將字符'B'追加到輸出流中
out.append('B');
// 將"CDE is 5" + 回車 寫入到輸出流中
String str = "CDE";
int num = 5;
out.printf("%s is %d\n", str, num);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
運行上面的代碼,會在源碼所在目錄生成兩個文件“file.txt”和“other.txt”。
file.txt的內容如下:
abcde
other.txt的內容如下:
hello PrintStream
A65BCDE is 5
來源:http://www.cnblogs.com/skywang12345/p/io_16.html