Java 中的Printstream介紹_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
PrintStream 介紹
PrintStream 是打印輸出流,它繼承于FilterOutputStream。
PrintStream 是用來裝飾其它輸出流。它能為其他輸出流添加了功能,使它們能夠方便地打印各種數(shù)據(jù)值表示形式。
與其他輸出流不同,PrintStream 永遠(yuǎn)不會(huì)拋出 IOException;它產(chǎn)生的IOException會(huì)被自身的函數(shù)所捕獲并設(shè)置錯(cuò)誤標(biāo)記, 用戶可以通過 checkError() 返回錯(cuò)誤標(biāo)記,從而查看PrintStream內(nèi)部是否產(chǎn)生了IOException。
另外,PrintStream 提供了自動(dòng)flush 和 字符集設(shè)置功能。所謂自動(dòng)flush,就是往PrintStream寫入的數(shù)據(jù)會(huì)立刻調(diào)用flush()函數(shù)。
PrintStream 函數(shù)列表
/* * 構(gòu)造函數(shù) */ // 將“輸出流out”作為PrintStream的輸出流,不會(huì)自動(dòng)flush,并且采用默認(rèn)字符集 // 所謂“自動(dòng)flush”,就是每次執(zhí)行print(), println(), write()函數(shù),都會(huì)調(diào)用flush()函數(shù); // 而“不自動(dòng)flush”,則需要我們手動(dòng)調(diào)用flush()接口。 PrintStream(OutputStream out) // 將“輸出流out”作為PrintStream的輸出流,自動(dòng)flush,并且采用默認(rèn)字符集。 PrintStream(OutputStream out, boolean autoFlush) // 將“輸出流out”作為PrintStream的輸出流,自動(dòng)flush,采用charsetName字符集。 PrintStream(OutputStream out, boolean autoFlush, String charsetName) // 創(chuàng)建file對(duì)應(yīng)的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動(dòng)flush,采用默認(rèn)字符集。 PrintStream(File file) // 創(chuàng)建file對(duì)應(yīng)的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動(dòng)flush,采用charsetName字符集。 PrintStream(File file, String charsetName) // 創(chuàng)建fileName對(duì)應(yīng)的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動(dòng)flush,采用默認(rèn)字符集。 PrintStream(String fileName) // 創(chuàng)建fileName對(duì)應(yīng)的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動(dòng)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輸出流緩沖中的數(shù)據(jù)”,并檢查錯(cuò)誤 boolean checkError() // 關(guān)閉“PrintStream輸出流” synchronized void close() // flush“PrintStream輸出流緩沖中的數(shù)據(jù)”。 // 例如,PrintStream裝飾的是FileOutputStream,則調(diào)用flush時(shí)會(huì)將數(shù)據(jù)寫入到文件中 synchronized void flush() // 根據(jù)“Locale值(區(qū)域?qū)傩?”來格式化數(shù)據(jù) PrintStream format(Locale l, String format, Object... args) // 根據(jù)“默認(rèn)的Locale值(區(qū)域?qū)傩?”來格式化數(shù)據(jù) PrintStream format(String format, Object... args) // 將“float數(shù)據(jù)f對(duì)應(yīng)的字符串”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù) void print(float f) // 將“double數(shù)據(jù)d對(duì)應(yīng)的字符串”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù) void print(double d) // 將“字符串?dāng)?shù)據(jù)str”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù) synchronized void print(String str) // 將“對(duì)象o對(duì)應(yīng)的字符串”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù) void print(Object o) // 將“字符c對(duì)應(yīng)的字符串”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù) void print(char c) // 將“字符數(shù)組chars對(duì)應(yīng)的字符串”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù) void print(char[] chars) // 將“l(fā)ong型數(shù)據(jù)l對(duì)應(yīng)的字符串”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù) void print(long l) // 將“int數(shù)據(jù)i對(duì)應(yīng)的字符串”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù) void print(int i) // 將“boolean數(shù)據(jù)b對(duì)應(yīng)的字符串”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù) void print(boolean b) // 將“數(shù)據(jù)args”根據(jù)“Locale值(區(qū)域?qū)傩?”按照format格式化,并寫入到“PrintStream輸出流”中 PrintStream printf(Locale l, String format, Object... args) // 將“數(shù)據(jù)args”根據(jù)“默認(rèn)Locale值(區(qū)域?qū)傩?”按照format格式化,并寫入到“PrintStream輸出流”中 PrintStream printf(String format, Object... args) // 將“換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù) void println() // 將“float數(shù)據(jù)對(duì)應(yīng)的字符串+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù) void println(float f) // 將“int數(shù)據(jù)對(duì)應(yīng)的字符串+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù) void println(int i) // 將“l(fā)ong數(shù)據(jù)對(duì)應(yīng)的字符串+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù) void println(long l) // 將“對(duì)象o對(duì)應(yīng)的字符串+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù) void println(Object o) // 將“字符數(shù)組chars對(duì)應(yīng)的字符串+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù) void println(char[] chars) // 將“字符串str+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù) synchronized void println(String str) // 將“字符c對(duì)應(yīng)的字符串+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù) void println(char c) // 將“double數(shù)據(jù)對(duì)應(yīng)的字符串+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù) void println(double d) // 將“boolean數(shù)據(jù)對(duì)應(yīng)的字符串+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù) void println(boolean b) // 將數(shù)據(jù)oneByte寫入到“PrintStream輸出流”中。oneByte雖然是int類型,但實(shí)際只會(huì)寫入一個(gè)字節(jié) synchronized void write(int oneByte) // 將“buffer中從offset開始的length個(gè)字節(jié)”寫入到“PrintStream輸出流”中。 void write(byte[] buffer, int offset, int length)
注意:print()和println()都是將其中參數(shù)轉(zhuǎn)換成字符串之后,再寫入到輸入流。
例如,
print(0x61);
等價(jià)于
write(String.valueOf(0x61));
上面語句是將字符串"97"寫入到輸出流。0x61對(duì)應(yīng)十進(jìn)制數(shù)是97。
write(0x61)
上面語句是將字符'a'寫入到輸出流。因?yàn)?x61對(duì)應(yīng)的ASCII碼的字母'a'。
查看下面的代碼,我們能對(duì)這些函數(shù)有更清晰的認(rèn)識(shí)!
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
{
// 自動(dòng)flush
// 所謂“自動(dòng)flush”,就是每次執(zhí)行print(), println(), write()函數(shù),都會(huì)調(diào)用flush()函數(shù);
// 而“不自動(dòng)flush”,則需要我們手動(dòng)調(diào)用flush()接口。
private final boolean autoFlush;
// PrintStream是否右產(chǎn)生異常。當(dāng)PrintStream有異常產(chǎn)生時(shí),會(huì)被本身捕獲,并設(shè)置trouble為true
private boolean trouble = false;
// 用于格式化的對(duì)象
private Formatter formatter;
// BufferedWriter對(duì)象,用于實(shí)現(xiàn)“PrintStream支持字符集”。
// 因?yàn)镻rintStream是OutputStream的子類,所以它本身不支持字符串;
// 但是BufferedWriter支持字符集,因此可以通過OutputStreamWriter創(chuàng)建PrintStream對(duì)應(yīng)的BufferedWriter對(duì)象,從而支持字符集。
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對(duì)應(yīng)的字符集對(duì)象
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模式,并且采用默認(rèn)字符集。
private PrintStream(boolean autoFlush, OutputStream out) {
super(out);
this.autoFlush = autoFlush;
this.charOut = new OutputStreamWriter(this);
this.textOut = new BufferedWriter(charOut);
}
// 將“輸出流out”作為PrintStream的輸出流,自動(dòng)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的輸出流,自動(dòng)flush,采用charsetName字符集。
private PrintStream(boolean autoFlush, Charset charset, OutputStream out)
throws UnsupportedEncodingException
{
this(autoFlush, out, charset);
}
// 將“輸出流out”作為PrintStream的輸出流,不會(huì)自動(dòng)flush,并且采用默認(rèn)字符集
public PrintStream(OutputStream out) {
this(out, false);
}
// 將“輸出流out”作為PrintStream的輸出流,自動(dòng)flush,并且采用默認(rèn)字符集。
public PrintStream(OutputStream out, boolean autoFlush) {
this(autoFlush, requireNonNull(out, "Null output stream"));
}
// 將“輸出流out”作為PrintStream的輸出流,自動(dòng)flush,采用charsetName字符集。
public PrintStream(OutputStream out, boolean autoFlush, String encoding)
throws UnsupportedEncodingException
{
this(autoFlush,
requireNonNull(out, "Null output stream"),
toCharset(encoding));
}
// 創(chuàng)建fileName對(duì)應(yīng)的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動(dòng)flush,采用默認(rèn)字符集。
public PrintStream(String fileName) throws FileNotFoundException {
this(false, new FileOutputStream(fileName));
}
// 創(chuàng)建fileName對(duì)應(yīng)的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動(dòng)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));
}
// 創(chuàng)建file對(duì)應(yīng)的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動(dòng)flush,采用默認(rèn)字符集。
public PrintStream(File file) throws FileNotFoundException {
this(false, new FileOutputStream(file));
}
// 創(chuàng)建file對(duì)應(yīng)的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動(dòng)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輸出流緩沖中的數(shù)據(jù)”。
// 例如,PrintStream裝飾的是FileOutputStream,則調(diào)用flush時(shí)會(huì)將數(shù)據(jù)寫入到文件中
public void flush() {
synchronized (this) {
try {
ensureOpen();
out.flush();
}
catch (IOException x) {
trouble = true;
}
}
}
private boolean closing = false; /* To avoid recursive closing */
// 關(guān)閉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輸出流緩沖中的數(shù)據(jù)”,并檢查錯(cuò)誤
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;
}
// 將數(shù)據(jù)b寫入到“PrintStream輸出流”中。b雖然是int類型,但實(shí)際只會(huì)寫入一個(gè)字節(jié)
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個(gè)字節(jié)”寫入到“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中的全部數(shù)據(jù)”寫入到“PrintStream輸出流”中。
private void write(char buf[]) {
try {
synchronized (this) {
ensureOpen();
textOut.write(buf);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush) {
for (int i = ; 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') >= ))
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數(shù)據(jù)對(duì)應(yīng)的字符串”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù)
public void print(boolean b) {
write(b ? "true" : "false");
}
// 將“字符c對(duì)應(yīng)的字符串”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù)
public void print(char c) {
write(String.valueOf(c));
}
// 將“int數(shù)據(jù)i對(duì)應(yīng)的字符串”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù)
public void print(int i) {
write(String.valueOf(i));
}
// 將“l(fā)ong型數(shù)據(jù)l對(duì)應(yīng)的字符串”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù)
public void print(long l) {
write(String.valueOf(l));
}
// 將“float數(shù)據(jù)f對(duì)應(yīng)的字符串”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù)
public void print(float f) {
write(String.valueOf(f));
}
// 將“double數(shù)據(jù)d對(duì)應(yīng)的字符串”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù)
public void print(double d) {
write(String.valueOf(d));
}
// 將“字符數(shù)組s”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù)
public void print(char s[]) {
write(s);
}
// 將“字符串?dāng)?shù)據(jù)s”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù)
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
// 將“對(duì)象obj對(duì)應(yīng)的字符串”寫入到“PrintStream輸出流”中,print實(shí)際調(diào)用的是write函數(shù)
public void print(Object obj) {
write(String.valueOf(obj));
}
// 將“換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù)
public void println() {
newLine();
}
// 將“boolean數(shù)據(jù)對(duì)應(yīng)的字符串+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù)
public void println(boolean x) {
synchronized (this) {
print(x);
newLine();
}
}
// 將“字符x對(duì)應(yīng)的字符串+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù)
public void println(char x) {
synchronized (this) {
print(x);
newLine();
}
}
// 將“int數(shù)據(jù)對(duì)應(yīng)的字符串+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù)
public void println(int x) {
synchronized (this) {
print(x);
newLine();
}
}
// 將“l(fā)ong數(shù)據(jù)對(duì)應(yīng)的字符串+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù)
public void println(long x) {
synchronized (this) {
print(x);
newLine();
}
}
// 將“float數(shù)據(jù)對(duì)應(yīng)的字符串+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù)
public void println(float x) {
synchronized (this) {
print(x);
newLine();
}
}
// 將“double數(shù)據(jù)對(duì)應(yīng)的字符串+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù)
public void println(double x) {
synchronized (this) {
print(x);
newLine();
}
}
// 將“字符數(shù)組x+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù)
public void println(char x[]) {
synchronized (this) {
print(x);
newLine();
}
}
// 將“字符串x+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù)
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
// 將“對(duì)象o對(duì)應(yīng)的字符串+換行符”寫入到“PrintStream輸出流”中,println實(shí)際調(diào)用的是write函數(shù)
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
// 將“數(shù)據(jù)args”根據(jù)“默認(rèn)Locale值(區(qū)域?qū)傩?”按照format格式化,并寫入到“PrintStream輸出流”中
public PrintStream printf(String format, Object ... args) {
return format(format, args);
}
// 將“數(shù)據(jù)args”根據(jù)“Locale值(區(qū)域?qū)傩?”按照format格式化,并寫入到“PrintStream輸出流”中
public PrintStream printf(Locale l, String format, Object ... args) {
return format(l, format, args);
}
// 根據(jù)“默認(rèn)的Locale值(區(qū)域?qū)傩?”來格式化數(shù)據(jù)
public PrintStream format(String format, Object ... args) {
try {
synchronized (this) {
ensureOpen();
if ((formatter == null)
|| (formatter.locale() != Locale.getDefault()))
formatter = new Formatter((Appendable) this);
formatter.format(Locale.getDefault(), format, args);
}
} catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
} catch (IOException x) {
trouble = true;
}
return this;
}
// 根據(jù)“Locale值(區(qū)域?qū)傩?”來格式化數(shù)據(jù)
public PrintStream format(Locale l, String format, Object ... args) {
try {
synchronized (this) {
ensureOpen();
if ((formatter == null)
|| (formatter.locale() != l))
formatter = new Formatter(this, l);
formatter.format(l, format, args);
}
} catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
} catch (IOException x) {
trouble = true;
}
return this;
}
// 將“字符序列的全部字符”追加到“PrintStream輸出流中”
public PrintStream append(CharSequence csq) {
if (csq == null)
print("null");
else
print(csq.toString());
return this;
}
// 將“字符序列從start(包括)到end(不包括)的全部字符”追加到“PrintStream輸出流中”
public PrintStream append(CharSequence csq, int start, int end) {
CharSequence cs = (csq == null ? "null" : csq);
write(cs.subSequence(start, end).toString());
return this;
}
// 將“字符c”追加到“PrintStream輸出流中”
public PrintStream append(char c) {
print(c);
return this;
}
}
說明:
PrintStream的源碼比較簡(jiǎn)單,請(qǐng)上文的注釋進(jìn)行閱讀。若有不明白的地方,建議先看看后面的PrintStream使用示例;待搞清它的作用和用法之后,再來閱讀源碼。
PrintStream和DataOutputStream異同點(diǎn)
相同點(diǎn):都是繼承與FileOutputStream,用于包裝其它輸出流。
不同點(diǎn):
(01) PrintStream和DataOutputStream 都可以將數(shù)據(jù)格式化輸出;但它們?cè)凇拜敵鲎址睍r(shí)的編碼不同。
PrintStream是輸出時(shí)采用的是用戶指定的編碼(創(chuàng)建PrintStream時(shí)指定的),若沒有指定,則采用系統(tǒng)默認(rèn)的字符編碼。而DataOutputStream則采用的是UTF-8。
(02) 它們的寫入數(shù)據(jù)時(shí)的異常處理機(jī)制不同。
DataOutputStream在通過write()向“輸出流”中寫入數(shù)據(jù)時(shí),若產(chǎn)生IOException,會(huì)拋出。
而PrintStream在通過write()向“輸出流”中寫入數(shù)據(jù)時(shí),若產(chǎn)生IOException,則會(huì)在write()中進(jìn)行捕獲處理;并設(shè)置trouble標(biāo)記(用于表示產(chǎn)生了異常)為true。用戶可以通過checkError()返回trouble值,從而檢查輸出流中是否產(chǎn)生了異常。
(03) 構(gòu)造函數(shù)不同
DataOutputStream的構(gòu)造函數(shù)只有一個(gè):DataOutputStream(OutputStream out)。即它只支持以輸出流out作為“DataOutputStream的輸出流”。
而PrintStream的構(gòu)造函數(shù)有許多:和DataOutputStream一樣,支持以輸出流out作為“PrintStream輸出流”的構(gòu)造函數(shù);還支持以“File對(duì)象”或者“String類型的文件名對(duì)象”的構(gòu)造函數(shù)。
而且,在PrintStream的構(gòu)造函數(shù)中,能“指定字符集”和“是否支持自動(dòng)flush()操作”。
(04) 目的不同
DataOutputStream的作用是裝飾其它的輸出流,它和DataInputStream配合使用:允許應(yīng)用程序以與機(jī)器無關(guān)的方式從底層輸入流中讀寫java數(shù)據(jù)類型。
而PrintStream的作用雖然也是裝飾其他輸出流,但是它的目的不是以與機(jī)器無關(guān)的方式從底層讀寫java數(shù)據(jù)類型;而是為其它輸出流提供打印各種數(shù)據(jù)值表示形式,使其它輸出流能方便的通過print(), println()或printf()等輸出各種格式的數(shù)據(jù)。
示例代碼
關(guān)于PrintStream中API的詳細(xì)用法,參考示例代碼(PrintStreamTest.java):
import java.io.PrintStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* PrintStream 的示例程序
*
*
*/
public class PrintStreamTest {
public static void main(String[] args) {
// 下面3個(gè)函數(shù)的作用都是一樣:都是將字母“abcde”寫入到文件“file.txt”中。
// 任選一個(gè)執(zhí)行即可!
testPrintStreamConstrutor() ;
//testPrintStreamConstrutor2() ;
//testPrintStreamConstrutor3() ;
// 測(cè)試write(), print(), println(), printf()等接口。
testPrintStreamAPIS() ;
}
/**
* PrintStream(OutputStream out) 的測(cè)試函數(shù)
*
* 函數(shù)的作用,就是將字母“abcde”寫入到文件“file.txt”中
*/
private static void testPrintStreamConstrutor() {
// 0x61對(duì)應(yīng)ASCII碼的字母'a',0x62對(duì)應(yīng)ASCII碼的字母'b', ...
final byte[] arr={0x61, 0x62, 0x63, 0x64, 0x65 }; // abced
try {
// 創(chuàng)建文件“file.txt”的File對(duì)象
File file = new File("file.txt");
// 創(chuàng)建文件對(duì)應(yīng)FileOutputStream
PrintStream out = new PrintStream(
new FileOutputStream(file));
// 將“字節(jié)數(shù)組arr”全部寫入到輸出流中
out.write(arr);
// 關(guān)閉輸出流
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* PrintStream(File file) 的測(cè)試函數(shù)
*
* 函數(shù)的作用,就是將字母“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) 的測(cè)試函數(shù)
*
* 函數(shù)的作用,就是將字母“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();
}
}
/**
* 測(cè)試write(), print(), println(), printf()等接口。
*/
private static void testPrintStreamAPIS() {
// 0x61對(duì)應(yīng)ASCII碼的字母'a',0x62對(duì)應(yīng)ASCII碼的字母'b', ...
final byte[] arr={0x61, 0x62, 0x63, 0x64, 0x65 }; // abced
try {
// 創(chuàng)建文件對(duì)應(yīng)FileOutputStream
PrintStream out = new PrintStream("other.txt");
// 將字符串“hello PrintStream”+回車符,寫入到輸出流中
out.println("hello PrintStream");
// 將x寫入到輸出流中
// x對(duì)應(yīng)ASCII碼的字母'A',也就是寫入字符'A'
out.write(0x41);
// 將字符串"65"寫入到輸出流中。
// out.print(0x41); 等價(jià)于 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();
}
}
}
運(yùn)行上面的代碼,會(huì)在源碼所在目錄生成兩個(gè)文件“file.txt”和“other.txt”。
file.txt的內(nèi)容如下:
abcde
other.txt的內(nèi)容如下:
hello PrintStream A65BCDE is 5
以上所述是小編給大家介紹的Java 中的Printstream知識(shí),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持
相關(guān)文章
SpringCloud融入Python的實(shí)現(xiàn)
這篇文章主要介紹了SpringCloud融入Python的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
java前后端傳值,參數(shù)有集合類型的數(shù)據(jù)時(shí)的兩種操作方式
這篇文章主要介紹了java前后端傳值,參數(shù)有集合類型的數(shù)據(jù)時(shí)的兩種操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
在spring?boot3中使用native?image的最新方法
這篇文章主要介紹了在spring?boot3中使用native?image?,今天我們用具體的例子來給大家演示一下如何正確的將spring boot3的應(yīng)用編譯成為native image,需要的朋友可以參考下2023-01-01
SpringBoot?整合?Spring-Session?實(shí)現(xiàn)分布式會(huì)話項(xiàng)目實(shí)戰(zhàn)
本文主要介紹了SpringBoot?整合?Spring-Session?實(shí)現(xiàn)分布式會(huì)話項(xiàng)目實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Java SMM框架關(guān)聯(lián)關(guān)系映射示例講解
SSM框架是spring MVC ,spring和mybatis框架的整合,是標(biāo)準(zhǔn)的MVC模式,將整個(gè)系統(tǒng)劃分為表現(xiàn)層,controller層,service層,DAO層四層,使用spring MVC負(fù)責(zé)請(qǐng)求的轉(zhuǎn)發(fā)和視圖管理,spring實(shí)現(xiàn)業(yè)務(wù)對(duì)象管理,mybatis作為數(shù)據(jù)對(duì)象的持久化引擎2022-08-08
Mybatis-plus操作json字段實(shí)戰(zhàn)教程
這篇文章主要介紹了Mybatis-plus操作json字段實(shí)戰(zhàn)教程,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02
Java創(chuàng)建型設(shè)計(jì)模式之單例模式
Java單例模式是一種設(shè)計(jì)模式,它確保一個(gè)類只有一個(gè)實(shí)例,并提供一個(gè)全局訪問點(diǎn)??梢允褂枚喾N方式實(shí)現(xiàn)單例模式,如餓漢式、懶漢式、雙重檢查鎖定、靜態(tài)內(nèi)部類、枚舉等,每種方式都有其優(yōu)缺點(diǎn),需要根據(jù)具體情況選擇使用2023-05-05
Java中controller層如何接收帶參數(shù)的查詢
本文主要介紹了Java中controller層如何接收帶參數(shù)的查詢,在控制器層接收帶參數(shù)的查詢可以通過多種方式實(shí)現(xiàn),下面就詳細(xì)的介紹一下,感興趣的可以了解一下2023-08-08

