发布于2020-11-19 21:04 阅读(1108) 评论(0) 点赞(16) 收藏(5)
目录
字段autoFlush,trouble,formatter,textOut,charOut,方法requireNonNull,toCharset
字段closing,方法ensureOpen,flush,close,checkError,setError,clearError
2个公共write方法,2个私有write方法,newLine方法
2个printf方法,2个format方法,3个append方法
- 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;
-
- /**
- * PrintStream向另一个输出流添加功能,即方便地打印各种数据值的表示的能力。
- * 还提供了其他两个特性。
- * 与其他outputstream不同,PrintStream从不抛出IOException;
- * 相反,异常情况仅仅设置了一个可以通过checkError方法进行测试的内部标志。
- * 可选地,可以创建一个自动刷新的PrintStream;
- * 这意味着在写入字节数组、调用println方法之一或写入换行字符或字节('\n')之后自动调用flush方法。
- *
- * <p> PrintStream打印的所有字符都使用平台的默认字符编码转换为字节。
- * 应该在需要写入字符而不是字节的情况下使用PrintWriter类。
- *
- * @author Frank Yellin
- * @author Mark Reinhold
- * @since JDK1.0
- */
-
- public class PrintStream extends FilterOutputStream
- implements Appendable, Closeable
- // 是否自动刷新的标志
- private final boolean autoFlush;
- // 表示类内部是否报错的标志
- private boolean trouble = false;
- private Formatter formatter;
-
- /**
- * 同时跟踪文本输出流和字符输出流,
- * 以便在不刷新整个流的情况下刷新它们的缓冲区。
- */
- private BufferedWriter textOut;
- private OutputStreamWriter charOut;
-
- /**
- * 在这里显式声明requireNonNull,以避免在系统初始化期间加载java.util.Objects.requireNonNull。
- */
- private static <T> T requireNonNull(T obj, String message) {
- if (obj == null)
- throw new NullPointerException(message);
- return obj;
- }
-
- /**
- * 返回给定字符集名称的Charset对象。
- * @throws NullPointerException is csn is null
- * @throws UnsupportedEncodingException if the charset is not supported
- */
- private static Charset toCharset(String csn)
- throws UnsupportedEncodingException
- {
- requireNonNull(csn, "charsetName");
- try {
- // 调用Charset的方法
- return Charset.forName(csn);
- } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
- // UnsupportedEncodingException should be thrown
- throw new UnsupportedEncodingException(csn);
- }
- }
- /* 私有构造器 */
- private PrintStream(boolean autoFlush, OutputStream out) {
- // 以out作为底层的out
- super(out);
- // 设置自动刷新
- this.autoFlush = autoFlush;
- // 把自己,PrintStream作为参数,给OutputStreamWriter,然后赋值给charOut
- // charOut = new OutputStreamWriter(new PrintStream(out))
- this.charOut = new OutputStreamWriter(this);
- // 把charOut,OutputStreamWriter作为参数,给BufferedWriter,然后赋值给textOut
- // textOut = new BufferedWriter(new OutputStreamWriter(new PrintStream(out)))
- this.textOut = new BufferedWriter(charOut);
- }
-
- private PrintStream(boolean autoFlush, OutputStream out, Charset charset) {
- super(out);
- this.autoFlush = autoFlush;
- // charset作为参数,传递给OutputStreamWriter,charOut
- this.charOut = new OutputStreamWriter(this, charset);
- this.textOut = new BufferedWriter(charOut);
- }
-
- /* 私有构造函数的变体,以便在计算OutputStream实参之前验证给定的字符集名称。
- * 由构造函数使用,它创建一个带有字符集名称的FileOutputStream。
- */
- private PrintStream(boolean autoFlush, Charset charset, OutputStream out)
- throws UnsupportedEncodingException
- {
- this(autoFlush, out, charset);
- }
-
- /**
- * 创建一个新的打印流。此流不会自动刷新。
- *
- * @param out The output stream to which values and objects will be
- * printed
- *
- * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream)
- */
- public PrintStream(OutputStream out) {
- this(out, false);
- }
-
- /**
- * 创建一个新的打印流。
- *
- * @param out The output stream to which values and objects will be
- * printed
- * @param autoFlush A boolean; if true, the output buffer will be flushed
- * whenever a byte array is written, one of the
- * <code>println</code> methods is invoked, or a newline
- * character or byte (<code>'\n'</code>) is written
- *
- * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
- */
- public PrintStream(OutputStream out, boolean autoFlush) {
- this(autoFlush, requireNonNull(out, "Null output stream"));
- }
-
- /**
- * 创建一个新的打印流。
- *
- * @param out The output stream to which values and objects will be
- * printed
- * @param autoFlush A boolean; if true, the output buffer will be flushed
- * whenever a byte array is written, one of the
- * <code>println</code> methods is invoked, or a newline
- * character or byte (<code>'\n'</code>) is written
- * @param encoding The name of a supported
- * <a href="../lang/package-summary.html#charenc">
- * character encoding</a>
- *
- * @throws UnsupportedEncodingException
- * If the named encoding is not supported
- *
- * @since 1.4
- */
- public PrintStream(OutputStream out, boolean autoFlush, String encoding)
- throws UnsupportedEncodingException
- {
- this(autoFlush,
- requireNonNull(out, "Null output stream"),
- toCharset(encoding));
- }
-
- /**
- * 使用指定的文件名创建一个新的打印流,不需要自动刷新行。
- * 这个方便的构造函数创建必要的中间值OutputStreamWriter,
- * 它将使用Java虚拟机的这个实例的默认charset对字符进行编码。
- *
- * @param fileName
- * The name of the file to use as the destination of this print
- * stream. 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 file object 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 PrintStream(String fileName) throws FileNotFoundException {
- this(false, new FileOutputStream(fileName));
- }
-
- /**
- * 使用指定的文件名创建一个新的打印流,不需要自动刷新行。
- * 这个方便的构造函数创建必要的中间值OutputStreamWriter,
- * 它将使用提供的charset编码字符。
- *
- * @param fileName
- * The name of the file to use as the destination of this print
- * stream. 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 file object 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 PrintStream(String fileName, String csn)
- throws FileNotFoundException, UnsupportedEncodingException
- {
- // ensure charset is checked before the file is opened
- this(false, toCharset(csn), new FileOutputStream(fileName));
- }
-
- /**
- * 使用指定的文件创建一个新的打印流,不需要自动刷新行。
- * 这个方便的构造函数创建了必要的中间值OutputStreamWriter,
- * 它将使用Java虚拟机的这个实例的默认字符集对字符进行编码。
- *
- * @param file
- * The file to use as the destination of this print stream. 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 file object 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(file.getPath())}
- * denies write access to the file
- *
- * @since 1.5
- */
- public PrintStream(File file) throws FileNotFoundException {
- this(false, new FileOutputStream(file));
- }
-
- /**
- * 使用特定的文件和字符集创建一个新的打印流,不需要自动刷新行。
- * 这个方便的构造函数创建必要的中间OutputStreamWriter,
- * 它将使用提供的charset编码字符。
- *
- * @param file
- * The file to use as the destination of this print stream. 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 file object 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(file.getPath())}
- * denies write access to the file
- *
- * @throws UnsupportedEncodingException
- * If the named charset is not supported
- *
- * @since 1.5
- */
- 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");
- }
-
- /**
- * 刷新流。
- * 这是通过将任何缓冲的输出字节写入底层输出流,然后刷新该流来实现的。
- *
- * @see java.io.OutputStream#flush()
- */
- public void flush() {
- synchronized (this) {
- try {
- // flush底层的out
- ensureOpen();
- out.flush();
- }
- catch (IOException x) {
- trouble = true;
- }
- }
- }
-
- private boolean closing = false; /* 避免递归关闭 */
-
- /**
- * 关闭流。这是通过刷新流,然后关闭底层输出流来完成的。
- *
- * @see java.io.OutputStream#close()
- */
- public void close() {
- synchronized (this) {
- if (! closing) {
- closing = true;
- try {
- // 关闭textOut,意味着关闭charOut,同时再次关闭PrintStream,遇到closing,结束
- textOut.close();
- // 关闭底层out
- out.close();
- }
- catch (IOException x) {
- trouble = true;
- }
- // 设置3个out为null
- textOut = null;
- charOut = null;
- out = null;
- }
- }
- }
-
- /**
- * 刷新流并检查其错误状态。
- * 当底层输出流抛出除InterruptedIOException之外的IOException时,并且当调用setError方法时,内部错误状态设置为true。
- * 如果底层输出流上的操作抛出InterruptedIOException,则printstream会通过以下操作将异常转换回中断:
- * <pre>
- * Thread.currentThread().interrupt();
- * </pre>
- * 或者是等价的。
- *
- * @return <code>true</code> if and only if this stream has encountered an
- * <code>IOException</code> other than
- * <code>InterruptedIOException</code>, or the
- * <code>setError</code> method has been invoked
- */
- public boolean checkError() {
- if (out != null)
- flush();
- if (out instanceof java.io.PrintStream) {
- PrintStream ps = (PrintStream) out;
- return ps.checkError();
- }
- return trouble;
- }
-
- /**
- * 将流的错误状态设置为true。
- *
- * <p> 这个方法将导致随后对checkError()的调用返回true,直到调用clearError()。
- *
- * @since JDK1.1
- */
- protected void setError() {
- trouble = true;
- }
-
- /**
- * 清除此流的内部错误状态。
- *
- * <p> 此方法将导致后续对checkError()的调用返回false,直到另一个写操作失败并调用setError()。
- *
- * @since 1.6
- */
- protected void clearError() {
- trouble = false;
- }
- /*
- * Exception-catching, synchronized output operations,
- * which also implement the write() methods of OutputStream
- */
-
- /**
- * 将指定的字节写入此流。
- * 如果字节是换行符,并且启用了自动刷新,那么将调用flush方法。
- *
- * <p> 注意,字节是按给定的方式写入的;
- * 要编写一个根据平台的默认字符编码进行翻译的字符,可以使用print(char)或println(char)方法。
- *
- * @param b The byte to be written
- * @see #print(char)
- * @see #println(char)
- */
- public void write(int b) {
- try {
- synchronized (this) {
- ensureOpen();
- // 底层out写入b
- out.write(b);
- // 对out进行刷新
- if ((b == '\n') && autoFlush)
- out.flush();
- }
- }
- catch (InterruptedIOException x) {
- Thread.currentThread().interrupt();
- }
- catch (IOException x) {
- trouble = true;
- }
- }
-
- /**
- * 将指定字节数组中的len字节从off开始写入到此流。
- * 如果启用了自动刷新,那么将调用flush方法。
- *
- * <p> 注意,字节将按给定的方式写入;
- * 要编写将根据平台的默认字符编码进行翻译的字符,请使用print(char)或println(char)方法。
- *
- * @param buf A byte array
- * @param off Offset from which to start taking bytes
- * @param len Number of bytes to write
- */
- 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;
- }
- }
-
- /*
- * 文本和字符输出流上的以下私有方法总是刷新流缓冲区,
- * 因此对底层字节流的写操作与原始PrintStream一样及时。
- */
-
- private void write(char buf[]) {
- try {
- synchronized (this) {
- ensureOpen();
- // textOut写入char数组,意味着BufferedWriter,OutputStreamWriter,PrintStream,out都写入了这个char数组
- textOut.write(buf);
- // 都刷新缓存
- textOut.flushBuffer();
- charOut.flushBuffer();
- if (autoFlush) {
- for (int i = 0; i < buf.length; i++)
- // 如果有一个char为\n,刷新底层流
- if (buf[i] == '\n')
- out.flush();
- }
- }
- }
- catch (InterruptedIOException x) {
- Thread.currentThread().interrupt();
- }
- catch (IOException x) {
- trouble = true;
- }
- }
-
- private void write(String s) {
- try {
- synchronized (this) {
- ensureOpen();
- // 写入textOut
- 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;
- }
- }
-
- 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;
- }
- }
- /* 不终止行的方法 */
-
- /**
- * 输出一个布尔值。java.lang.String.valueOf(boolean)生成的字符串根据
- * 平台的默认字符编码被转换成字节,并且这些字节完全按照write(int)方法的方式被写入。
- *
- * @param b The <code>boolean</code> to be printed
- */
- public void print(boolean b) {
- write(b ? "true" : "false");
- }
-
- /**
- * 输出一个字符。
- * 根据平台的默认字符编码,字符被转换成一个或多个字节码,这些字节码完全按照write(int)方法的方式写入。
- *
- * @param c The <code>char</code> to be printed
- */
- public void print(char c) {
- write(String.valueOf(c));
- }
-
- /**
- * Prints an integer. The string produced by <code>{@link
- * java.lang.String#valueOf(int)}</code> is translated into bytes
- * according to the platform's default character encoding, and these bytes
- * are written in exactly the manner of the
- * <code>{@link #write(int)}</code> method.
- *
- * @param i The <code>int</code> to be printed
- * @see java.lang.Integer#toString(int)
- */
- public void print(int i) {
- write(String.valueOf(i));
- }
-
- /**
- * Prints a long integer. The string produced by <code>{@link
- * java.lang.String#valueOf(long)}</code> is translated into bytes
- * according to the platform's default character encoding, and these bytes
- * are written in exactly the manner of the
- * <code>{@link #write(int)}</code> method.
- *
- * @param l The <code>long</code> to be printed
- * @see java.lang.Long#toString(long)
- */
- public void print(long l) {
- write(String.valueOf(l));
- }
-
- /**
- * Prints a floating-point number. The string produced by <code>{@link
- * java.lang.String#valueOf(float)}</code> is translated into bytes
- * according to the platform's default character encoding, and these bytes
- * are written in exactly the manner of the
- * <code>{@link #write(int)}</code> method.
- *
- * @param f The <code>float</code> to be printed
- * @see java.lang.Float#toString(float)
- */
- public void print(float f) {
- write(String.valueOf(f));
- }
-
- /**
- * Prints a double-precision floating-point number. The string produced by
- * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
- * bytes according to the platform's default character encoding, and these
- * bytes are written in exactly the manner of the <code>{@link
- * #write(int)}</code> method.
- *
- * @param d The <code>double</code> to be printed
- * @see java.lang.Double#toString(double)
- */
- public void print(double d) {
- write(String.valueOf(d));
- }
-
- /**
- * Prints an array of characters. The characters are converted into bytes
- * according to the platform's default character encoding, and these bytes
- * are written in exactly the manner of the
- * <code>{@link #write(int)}</code> method.
- *
- * @param s The array of chars to be printed
- *
- * @throws NullPointerException If <code>s</code> is <code>null</code>
- */
- public void print(char s[]) {
- write(s);
- }
-
- /**
- * Prints a string. If the argument is <code>null</code> then the string
- * <code>"null"</code> is printed. Otherwise, the string's characters are
- * converted into bytes according to the platform's default character
- * encoding, and these bytes are written in exactly the manner of the
- * <code>{@link #write(int)}</code> method.
- *
- * @param s The <code>String</code> to be printed
- */
- public void print(String s) {
- if (s == null) {
- s = "null";
- }
- write(s);
- }
-
- /**
- * Prints an object. The string produced by the <code>{@link
- * java.lang.String#valueOf(Object)}</code> method is translated into bytes
- * according to the platform's default character encoding, and these bytes
- * are written in exactly the manner of the
- * <code>{@link #write(int)}</code> method.
- *
- * @param obj The <code>Object</code> to be printed
- * @see java.lang.Object#toString()
- */
- public void print(Object obj) {
- write(String.valueOf(obj));
- }
- /* 终止行的方法 */
-
- /**
- * 通过写入行分隔符字符串终止当前行。
- * 线分隔符字符串由系统属性line.separator定义。
- * 而不一定是单个换行字符 ('\n')。
- */
- public void println() {
- newLine();
- }
-
- /**
- * 输出一个布尔值,然后终止行。
- * 这个方法的行为就像它调用print(boolean)然后调用println()一样。
- *
- * @param x The <code>boolean</code> to be printed
- */
- public void println(boolean x) {
- synchronized (this) {
- print(x);
- newLine();
- }
- }
-
- /**
- * Prints a character and then terminate the line. This method behaves as
- * though it invokes <code>{@link #print(char)}</code> and then
- * <code>{@link #println()}</code>.
- *
- * @param x The <code>char</code> to be printed.
- */
- public void println(char x) {
- synchronized (this) {
- print(x);
- newLine();
- }
- }
-
- /**
- * Prints an integer and then terminate the line. This method behaves as
- * though it invokes <code>{@link #print(int)}</code> and then
- * <code>{@link #println()}</code>.
- *
- * @param x The <code>int</code> to be printed.
- */
- public void println(int x) {
- synchronized (this) {
- print(x);
- newLine();
- }
- }
-
- /**
- * Prints a long and then terminate the line. This method behaves as
- * though it invokes <code>{@link #print(long)}</code> and then
- * <code>{@link #println()}</code>.
- *
- * @param x a The <code>long</code> to be printed.
- */
- public void println(long x) {
- synchronized (this) {
- print(x);
- newLine();
- }
- }
-
- /**
- * Prints a float and then terminate the line. This method behaves as
- * though it invokes <code>{@link #print(float)}</code> and then
- * <code>{@link #println()}</code>.
- *
- * @param x The <code>float</code> to be printed.
- */
- public void println(float x) {
- synchronized (this) {
- print(x);
- newLine();
- }
- }
-
- /**
- * Prints a double and then terminate the line. This method behaves as
- * though it invokes <code>{@link #print(double)}</code> and then
- * <code>{@link #println()}</code>.
- *
- * @param x The <code>double</code> to be printed.
- */
- public void println(double x) {
- synchronized (this) {
- print(x);
- newLine();
- }
- }
-
- /**
- * Prints an array of characters and then terminate the line. This method
- * behaves as though it invokes <code>{@link #print(char[])}</code> and
- * then <code>{@link #println()}</code>.
- *
- * @param x an array of chars to print.
- */
- public void println(char x[]) {
- synchronized (this) {
- print(x);
- newLine();
- }
- }
-
- /**
- * Prints a String and then terminate the line. This method behaves as
- * though it invokes <code>{@link #print(String)}</code> and then
- * <code>{@link #println()}</code>.
- *
- * @param x The <code>String</code> to be printed.
- */
- public void println(String x) {
- synchronized (this) {
- print(x);
- newLine();
- }
- }
-
- /**
- * Prints an Object and then terminate the line. This method calls
- * at first String.valueOf(x) to get the printed object's string value,
- * then behaves as
- * though it invokes <code>{@link #print(String)}</code> and then
- * <code>{@link #println()}</code>.
- *
- * @param x The <code>Object</code> to be printed.
- */
- public void println(Object x) {
- String s = String.valueOf(x);
- synchronized (this) {
- print(s);
- newLine();
- }
- }
- /**
- * 使用指定的格式字符串和参数将格式化字符串写入输出流的方便方法。
- *
- * <p> 调用out.printf(format,args)形式的这个方法的行为,与调用下面的方式完全相同。
- *
- * <pre>
- * out.format(format, args) </pre>
- *
- * @param format
- * A format string as described in <a
- * href="../util/Formatter.html#syntax">Format string syntax</a>
- *
- * @param args
- * Arguments referenced by the format specifiers in the format
- * string. If there are more arguments than format specifiers, the
- * extra arguments are ignored. The number of arguments is
- * variable and may be zero. The maximum number of arguments is
- * limited by the maximum dimension of a Java array as defined by
- * <cite>The Java™ Virtual Machine Specification</cite>.
- * The behaviour on a
- * <tt>null</tt> argument depends on the <a
- * href="../util/Formatter.html#syntax">conversion</a>.
- *
- * @throws java.util.IllegalFormatException
- * If a format string contains an illegal syntax, a format
- * specifier that is incompatible with the given arguments,
- * insufficient arguments given the format string, or other
- * illegal conditions. For specification of all possible
- * formatting errors, see the <a
- * href="../util/Formatter.html#detail">Details</a> section of the
- * formatter class specification.
- *
- * @throws NullPointerException
- * If the <tt>format</tt> is <tt>null</tt>
- *
- * @return This output stream
- *
- * @since 1.5
- */
- public PrintStream printf(String format, Object ... args) {
- return format(format, args);
- }
-
- /**
- * 使用指定的格式字符串和参数将格式化字符串写入输出流的方便方法。
- *
- * <p> 调用out.printf(l, format,args)形式的这个方法的行为,与调用下面的方式完全相同。
- *
- * <pre>
- * out.format(l, format, args) </pre>
- *
- * @param l
- * The {@linkplain java.util.Locale locale} to apply during
- * formatting. If <tt>l</tt> is <tt>null</tt> then no localization
- * is applied.
- *
- * @param format
- * A format string as described in <a
- * href="../util/Formatter.html#syntax">Format string syntax</a>
- *
- * @param args
- * Arguments referenced by the format specifiers in the format
- * string. If there are more arguments than format specifiers, the
- * extra arguments are ignored. The number of arguments is
- * variable and may be zero. The maximum number of arguments is
- * limited by the maximum dimension of a Java array as defined by
- * <cite>The Java™ Virtual Machine Specification</cite>.
- * The behaviour on a
- * <tt>null</tt> argument depends on the <a
- * href="../util/Formatter.html#syntax">conversion</a>.
- *
- * @throws java.util.IllegalFormatException
- * If a format string contains an illegal syntax, a format
- * specifier that is incompatible with the given arguments,
- * insufficient arguments given the format string, or other
- * illegal conditions. For specification of all possible
- * formatting errors, see the <a
- * href="../util/Formatter.html#detail">Details</a> section of the
- * formatter class specification.
- *
- * @throws NullPointerException
- * If the <tt>format</tt> is <tt>null</tt>
- *
- * @return This output stream
- *
- * @since 1.5
- */
- public PrintStream printf(Locale l, String format, Object ... args) {
- return format(l, format, args);
- }
-
- /**
- * 使用指定格式字符串和参数将格式化字符串写入输出流。
- *
- * <p> 始终使用的语言环境是Locale.getDefault()返回的语言环境,
- * 而不考虑之前对该对象的其他格式化方法的调用。
- *
- * @param format
- * A format string as described in <a
- * href="../util/Formatter.html#syntax">Format string syntax</a>
- *
- * @param args
- * Arguments referenced by the format specifiers in the format
- * string. If there are more arguments than format specifiers, the
- * extra arguments are ignored. The number of arguments is
- * variable and may be zero. The maximum number of arguments is
- * limited by the maximum dimension of a Java array as defined by
- * <cite>The Java™ Virtual Machine Specification</cite>.
- * The behaviour on a
- * <tt>null</tt> argument depends on the <a
- * href="../util/Formatter.html#syntax">conversion</a>.
- *
- * @throws java.util.IllegalFormatException
- * If a format string contains an illegal syntax, a format
- * specifier that is incompatible with the given arguments,
- * insufficient arguments given the format string, or other
- * illegal conditions. For specification of all possible
- * formatting errors, see the <a
- * href="../util/Formatter.html#detail">Details</a> section of the
- * formatter class specification.
- *
- * @throws NullPointerException
- * If the <tt>format</tt> is <tt>null</tt>
- *
- * @return This output stream
- *
- * @since 1.5
- */
- public PrintStream format(String format, Object ... args) {
- try {
- synchronized (this) {
- ensureOpen();
- if ((formatter == null)
- || (formatter.locale() != Locale.getDefault()))
- // 以自己作为终点,创建一个Formatter
- formatter = new Formatter((Appendable) this);
- // 使用特定的区域设置、格式字符串和参数将格式化字符串写入此对象的目标。
- formatter.format(Locale.getDefault(), format, args);
- }
- } catch (InterruptedIOException x) {
- Thread.currentThread().interrupt();
- } catch (IOException x) {
- trouble = true;
- }
- return this;
- }
-
- /**
- * 使用指定格式字符串和参数将格式化字符串写入输出流。
- *
- * @param l
- * The {@linkplain java.util.Locale locale} to apply during
- * formatting. If <tt>l</tt> is <tt>null</tt> then no localization
- * is applied.
- *
- * @param format
- * A format string as described in <a
- * href="../util/Formatter.html#syntax">Format string syntax</a>
- *
- * @param args
- * Arguments referenced by the format specifiers in the format
- * string. If there are more arguments than format specifiers, the
- * extra arguments are ignored. The number of arguments is
- * variable and may be zero. The maximum number of arguments is
- * limited by the maximum dimension of a Java array as defined by
- * <cite>The Java™ Virtual Machine Specification</cite>.
- * The behaviour on a
- * <tt>null</tt> argument depends on the <a
- * href="../util/Formatter.html#syntax">conversion</a>.
- *
- * @throws java.util.IllegalFormatException
- * If a format string contains an illegal syntax, a format
- * specifier that is incompatible with the given arguments,
- * insufficient arguments given the format string, or other
- * illegal conditions. For specification of all possible
- * formatting errors, see the <a
- * href="../util/Formatter.html#detail">Details</a> section of the
- * formatter class specification.
- *
- * @throws NullPointerException
- * If the <tt>format</tt> is <tt>null</tt>
- *
- * @return This output stream
- *
- * @since 1.5
- */
- 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;
- }
-
- /**
- * 将指定的字符序列追加到此输出流。
- *
- * <p> 对out.append(csq)这个方法的调用行为与下面完全相同
- *
- * <pre>
- * out.print(csq.toString()) </pre>
- *
- * <p> 根据字符序列csq的toString规范,整个序列可能不会被附加。
- * 例如,调用一个字符缓冲区的toString方法将返回一个子序列,其内容取决于缓冲区的位置和限制。
- *
- * @param csq
- * The character sequence to append. If <tt>csq</tt> is
- * <tt>null</tt>, then the four characters <tt>"null"</tt> are
- * appended to this output stream.
- *
- * @return This output stream
- *
- * @since 1.5
- */
- public PrintStream append(CharSequence csq) {
- if (csq == null)
- print("null");
- else
- print(csq.toString());
- return this;
- }
-
- /**
- * Appends a subsequence of the specified character sequence to this output
- * stream.
- *
- * <p> An invocation of this method of the form <tt>out.append(csq, start,
- * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
- * exactly the same way as the invocation
- *
- * <pre>
- * out.print(csq.subSequence(start, end).toString()) </pre>
- *
- * @param csq
- * The character sequence from which a subsequence will be
- * appended. If <tt>csq</tt> is <tt>null</tt>, then characters
- * will be appended as if <tt>csq</tt> contained the four
- * characters <tt>"null"</tt>.
- *
- * @param start
- * The index of the first character in the subsequence
- *
- * @param end
- * The index of the character following the last character in the
- * subsequence
- *
- * @return This output stream
- *
- * @throws IndexOutOfBoundsException
- * If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
- * is greater than <tt>end</tt>, or <tt>end</tt> is greater than
- * <tt>csq.length()</tt>
- *
- * @since 1.5
- */
- public PrintStream append(CharSequence csq, int start, int end) {
- CharSequence cs = (csq == null ? "null" : csq);
- write(cs.subSequence(start, end).toString());
- return this;
- }
-
- /**
- * Appends the specified character to this output stream.
- *
- * <p> An invocation of this method of the form <tt>out.append(c)</tt>
- * behaves in exactly the same way as the invocation
- *
- * <pre>
- * out.print(c) </pre>
- *
- * @param c
- * The 16-bit character to append
- *
- * @return This output stream
- *
- * @since 1.5
- */
- public PrintStream append(char c) {
- print(c);
- return this;
- }
原文链接:https://blog.csdn.net/xushiyu1996818/article/details/109724299
作者:javagogogo
链接:http://www.javaheidong.com/blog/article/919/97afd938ecc390fa7855/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!