目录
字段out,autoFlush,trouble,formatter,psOut,lineSeparator,toCharset方法
方法ensureOpen,flush,close,checkError,setError,clearError
2个printf方法,2个format方法,3个append方法
简介
package java.io;
import java.util.Objects;
import java.util.Formatter;
import java.util.Locale;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
/**
* 将对象的格式化表示形式打印到文本输出流。
* 这个类实现了在PrintStream中找到的所有打印方法。
* 它不包含写原始字节的方法,因为程序应该使用未编码的字节流。
*
* <p> 与PrintStream类不同的是,如果启用了自动刷新,
* 那么自动刷新只会在调用println、printf或format方法之一时进行,而不是在输出换行符时进行。
* 这些方法使用平台自己的line separator概念,而不是换行符。
*
* <p> 这个类中的方法永远不会抛出I/O异常,尽管它的一些构造函数可能会。
* 客户端可以通过调用checkError()来查询是否有错误发生。
*
* @author Frank Yellin
* @author Mark Reinhold
* @since JDK1.1
*/
public class PrintWriter extends Writer
字段out,autoFlush,trouble,formatter,psOut,lineSeparator,toCharset方法
/**
* 此PrintWriter的底层字符输出流。
*
* @since 1.2
*/
protected Writer out;
private final boolean autoFlush;
private boolean trouble = false;
private Formatter formatter;
private PrintStream psOut = null;
/**
* 行分隔符字符串。这是创建流时的line.separator值。。
*/
private final String lineSeparator;
/**
* 返回给定字符集名称的Charset对象。
* @throws NullPointerException is csn is null
* @throws UnsupportedEncodingException if the charset is not supported
*/
private static Charset toCharset(String csn)
throws UnsupportedEncodingException
{
Objects.requireNonNull(csn, "charsetName");
try {
return Charset.forName(csn);
} catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
// UnsupportedEncodingException should be thrown
throw new UnsupportedEncodingException(csn);
}
}
9个构造函数
/**
* 创建一个新的PrintWriter,不需要自动刷新行。
*
* @param out A character-output stream
*/
public PrintWriter (Writer out) {
this(out, false);
}
/**
* 创建一个新的PrintWriter。
*
* @param out A character-output stream
* @param autoFlush A boolean; if true, the <tt>println</tt>,
* <tt>printf</tt>, or <tt>format</tt> methods will
* flush the output buffer
*/
public PrintWriter(Writer out,
boolean autoFlush) {
// 以out为lock
super(out);
// 赋值out和autoFlush
this.out = out;
this.autoFlush = autoFlush;
// 赋值lineSeparator
lineSeparator = java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("line.separator"));
}
/**
* 从现有的OutputStream创建一个新的PrintWriter,不需要自动刷新行。
* 这个方便的构造函数创建了必要的中间值OutputStreamWriter,它将使用默认的字符编码将字符转换为字节。
*
* @param out An output stream
*
* @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
*/
public PrintWriter(OutputStream out) {
this(out, false);
}
/**
* 从现有的输出流创建一个新的PrintWriter。
* 这个方便的构造函数创建了必要的OutputStreamWriter,它将使用默认的字符编码将字符转换为字节。
*
* @param out An output stream
* @param autoFlush A boolean; if true, the <tt>println</tt>,
* <tt>printf</tt>, or <tt>format</tt> methods will
* flush the output buffer
*
* @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
*/
public PrintWriter(OutputStream out, boolean autoFlush) {
// 通过out创建OutputStreamWriter ,再创建BufferedWriter,再调用PrintWriter(Writer out, boolean autoFlush)
this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
// 保存打印流错误传播
if (out instanceof java.io.PrintStream) {
psOut = (PrintStream) out;
}
}
/**
* 使用指定的文件名创建一个新的PrintWriter,不需要自动刷新行。
* 这个方便的构造函数创建了必要的中间值OutputStreamWriter,
* 它将使用Java虚拟机的这个实例的默认字符集对字符进行编码。
*
* @param fileName
* The name of the file to use as the destination of this writer.
* If the file exists then it will be truncated to zero size;
* otherwise, a new file will be created. The output will be
* written to the file and is buffered.
*
* @throws FileNotFoundException
* If the given string does not denote an existing, writable
* regular file and a new regular file of that name cannot be
* created, or if some other error occurs while opening or
* creating the file
*
* @throws SecurityException
* If a security manager is present and {@link
* SecurityManager#checkWrite checkWrite(fileName)} denies write
* access to the file
*
* @since 1.5
*/
public PrintWriter(String fileName) throws FileNotFoundException {
this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
false);
}
/* 私有构造器 */
private PrintWriter(Charset charset, File file)
throws FileNotFoundException
{
this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)),
false);
}
/**
* 使用指定的文件名和字符集创建一个新的打印程序,不需要自动刷新行。
* 这个方便的构造函数创建必要的中间OutputStreamWriter,它将使用提供的charset编码字符。
*
* @param fileName
* The name of the file to use as the destination of this writer.
* If the file exists then it will be truncated to zero size;
* otherwise, a new file will be created. The output will be
* written to the file and is buffered.
*
* @param csn
* The name of a supported {@linkplain java.nio.charset.Charset
* charset}
*
* @throws FileNotFoundException
* If the given string does not denote an existing, writable
* regular file and a new regular file of that name cannot be
* created, or if some other error occurs while opening or
* creating the file
*
* @throws SecurityException
* If a security manager is present and {@link
* SecurityManager#checkWrite checkWrite(fileName)} denies write
* access to the file
*
* @throws UnsupportedEncodingException
* If the named charset is not supported
*
* @since 1.5
*/
public PrintWriter(String fileName, String csn)
throws FileNotFoundException, UnsupportedEncodingException
{
this(toCharset(csn), new File(fileName));
}
/**
* 使用指定的文件创建一个新的PrintWriter,不需要自动刷新行。
* 这个方便的构造函数创建了必要的中间值OutputStreamWriter,
* 它将使用Java虚拟机的这个实例的默认字符集对字符进行编码。
*
* @param file
* The file to use as the destination of this writer. If the file
* exists then it will be truncated to zero size;