声明:本系列只供本人自学使用,勿喷。
参考:https://www.cnblogs.com/skywang12345/p/io_16.html
- 打印输出流也是一种处理流(FilterOutputStream),用来装饰其它输出流
- PrintStream 永远不会抛出 IOException;它产生的IOException会被自身的函数所捕获并设置错误标记, 用户可以通过 checkError() 返回错误标记,从而查看PrintStream内部是否产生了IOException。
- 另外,PrintStream 提供了自动flush 和 字符集设置功能。所谓自动flush,就是往PrintStream写入的数据会立刻调用flush()函数。
- PrintWriter 是字符类型的打印输出流,它继承于Writer。
- PrintWriter 用于向文本输出流打印对象的格式化表示形式。它实现了PrintStream 的所有 print 方法。它不包含用于写入原始字节的方法,对于这些字节,程序应该使用未编码的字节流进行写入。
一、PrintStream
- 构造器
public class PrintStream extends FilterOutputStream implements Appendable, Closeable{
private final boolean autoFlush;
private BufferedWriter textOut;
private OutputStreamWriter charOut;
// 入参为 OutputStream
public PrintStream(OutputStream out)
public PrintStream(OutputStream out, boolean autoFlush)
public PrintStream(OutputStream out, boolean autoFlush, String encoding)
// 入参为 文件 或 文件名
public PrintStream(String fileName)
public PrintStream(String fileName, String csn)
public PrintStream(File file)
public PrintStream(File file, String csn)
//构造之后会初始化,
// 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()系列方法,实际调用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()系列方法,实际调用print()+newLine()
public void println() {
newLine();
}
public void println(boolean x) {
synchronized (this) {
print(x);
newLine();
}
}
// 实际调用 format(format, args);
public PrintStream printf(String format, Object ... args)
// 实际调用 format(l, format, args);
public PrintStream printf(Locale l, String format, Object ... args)
...
// append()系列方法,实际调用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
- 构造器
public class PrintWriter extends Writer {
private final boolean autoFlush;
private BufferedWriter textOut;
private OutputStreamWriter charOut;
无论什么入参都会转换为BufferedWriter
// 入参为 OutputStream
public PrintWriter(OutputStream out)
public PrintWriter(OutputStream out, boolean autoFlush)
// 入参为 Writer
public PrintWriter (Writer out)
public PrintWriter(Writer out,boolean autoFlush)
// 入参为 文件 或 文件名
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()系列方法,实际调用 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()系列方法,实际调用print()+newLine()
public void println() {
newLine();
}
public void println(boolean x) {
synchronized (this) {
print(x);
newLine();
}
}
// 实际调用 format(format, args);
public PrintStream printf(String format, Object ... args)
// 实际调用 format(l, format, args);
public PrintStream printf(Locale l, String format, Object ... args)
...
// append()系列方法,实际调用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的区别,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();
}
}
}