聲明:本系列只供本人自學(xué)使用,勿噴彪薛。
參考:https://www.cnblogs.com/skywang12345/p/io_16.html
- 打印輸出流也是一種處理流(FilterOutputStream),用來裝飾其它輸出流
- 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ù)豆茫。
- PrintWriter 是字符類型的打印輸出流侨歉,它繼承于Writer。
- PrintWriter 用于向文本輸出流打印對(duì)象的格式化表示形式揩魂。它實(shí)現(xiàn)了PrintStream 的所有 print 方法幽邓。它不包含用于寫入原始字節(jié)的方法,對(duì)于這些字節(jié)火脉,程序應(yīng)該使用未編碼的字節(jié)流進(jìn)行寫入牵舵。
一、PrintStream
- 構(gòu)造器
public class PrintStream extends FilterOutputStream implements Appendable, Closeable{
private final boolean autoFlush;
private BufferedWriter textOut;
private OutputStreamWriter charOut;
// 入?yún)?OutputStream
public PrintStream(OutputStream out)
public PrintStream(OutputStream out, boolean autoFlush)
public PrintStream(OutputStream out, boolean autoFlush, String encoding)
// 入?yún)?文件 或 文件名
public PrintStream(String fileName)
public PrintStream(String fileName, String csn)
public PrintStream(File file)
public PrintStream(File file, String csn)
//構(gòu)造之后會(huì)初始化倦挂,
// this.charOut = new OutputStreamWriter(out);
// this.textOut = new BufferedWriter(charOut);
}
- 核心方法
public void write(int b)
public void write(byte buf[], int off, int len)
// print()系列方法畸颅,實(shí)際調(diào)用private void write(String s)
public void print(boolean b)
public void print(char c)
public void print(int i)
public void print(long l)
public void print(float f)
public void print(double d)
...
private void write(String s) {
try {
synchronized (this) {
ensureOpen();
//用的是BufferedWriter的write方法
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;
}
}
// println()系列方法,實(shí)際調(diào)用print()+newLine()
public void println() {
newLine();
}
public void println(boolean x) {
synchronized (this) {
print(x);
newLine();
}
}
// 實(shí)際調(diào)用 format(format, args);
public PrintStream printf(String format, Object ... args)
// 實(shí)際調(diào)用 format(l, format, args);
public PrintStream printf(Locale l, String format, Object ... args)
...
// append()系列方法方援,實(shí)際調(diào)用print()方法
public PrintStream append(CharSequence csq)
public PrintStream append(CharSequence csq, int start, int end)
public PrintStream append(char c)
demo
public class Test {
public static void main(String[] args) {
try (
OutputStream os = Files.newOutputStream(Paths.get("D:\\PPPPP\\TestExcel\\src\\main\\resources\\app4.properties"));
PrintStream ps=new PrintStream(os);
) {
ps.write("hello 世界".getBytes());
ps.println("換行了");
ps.print(20.0);
ps.println("緊接在20.0之后");
ps.println();
ps.append('a');
} catch (IOException e) {
e.printStackTrace();
}
}
}
二没炒、PrintWriter
- 構(gòu)造器
public class PrintWriter extends Writer {
private final boolean autoFlush;
private BufferedWriter textOut;
private OutputStreamWriter charOut;
無論什么入?yún)⒍紩?huì)轉(zhuǎn)換為BufferedWriter
// 入?yún)?OutputStream
public PrintWriter(OutputStream out)
public PrintWriter(OutputStream out, boolean autoFlush)
// 入?yún)?Writer
public PrintWriter (Writer out)
public PrintWriter(Writer out,boolean autoFlush)
// 入?yún)?文件 或 文件名
public PrintStream(String fileName)
public PrintStream(String fileName, String csn)
public PrintStream(File file)
public PrintStream(File file, String csn)
}
- 核心方法
public void write(int b)
public void write(char buf[], int off, int len)
public void write(char buf[])
public void write(String s, int off, int len)
public void write(String s)
// print()系列方法,實(shí)際調(diào)用 public void write(String s, int off, int len)
public void print(boolean b)
public void print(char c)
public void print(int i)
public void print(long l)
public void print(float f)
public void print(double d)
...
// println()系列方法犯戏,實(shí)際調(diào)用print()+newLine()
public void println() {
newLine();
}
public void println(boolean x) {
synchronized (this) {
print(x);
newLine();
}
}
// 實(shí)際調(diào)用 format(format, args);
public PrintStream printf(String format, Object ... args)
// 實(shí)際調(diào)用 format(l, format, args);
public PrintStream printf(Locale l, String format, Object ... args)
...
// append()系列方法送火,實(shí)際調(diào)用print()方法
public PrintStream append(CharSequence csq)
public PrintStream append(CharSequence csq, int start, int end)
public PrintStream append(char c)
demo
public class Test {
public static void main(String[] args) {
try (
OutputStream os = Files.newOutputStream(Paths.get("D:\\PPPPP\\TestExcel\\src\\main\\resources\\app4.properties"));
PrintWriter pw=new PrintWriter(os);
) {
// 注意和PrintStream的區(qū)別,PrintStream是write byte[]笛丙,PrintWriter是write char[]
pw.write("hello 世界");
pw.println("換行了");
pw.print(20.0);
pw.println("緊接在20.0之后");
pw.println();
pw.append('a');
} catch (IOException e) {
e.printStackTrace();
}
}
}