本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2024-11(1)

java io PrintStream源码分析

发布于2020-11-19 21:04     阅读(1108)     评论(0)     点赞(16)     收藏(5)


目录

简介

字段autoFlush,trouble,formatter,textOut,charOut,方法requireNonNull,toCharset

3个私有构造函数,7个公共构造函数

字段closing,方法ensureOpen,flush,close,checkError,setError,clearError

2个公共write方法,2个私有write方法,newLine方法

9个print方法

10个println方法

2个printf方法,2个format方法,3个append方法


简介

  1. package java.io;
  2. import java.util.Formatter;
  3. import java.util.Locale;
  4. import java.nio.charset.Charset;
  5. import java.nio.charset.IllegalCharsetNameException;
  6. import java.nio.charset.UnsupportedCharsetException;
  7. /**
  8. * PrintStream向另一个输出流添加功能,即方便地打印各种数据值的表示的能力。
  9. * 还提供了其他两个特性。
  10. * 与其他outputstream不同,PrintStream从不抛出IOException;
  11. * 相反,异常情况仅仅设置了一个可以通过checkError方法进行测试的内部标志。
  12. * 可选地,可以创建一个自动刷新的PrintStream;
  13. * 这意味着在写入字节数组、调用println方法之一或写入换行字符或字节('\n')之后自动调用flush方法。
  14. *
  15. * <p> PrintStream打印的所有字符都使用平台的默认字符编码转换为字节。
  16. * 应该在需要写入字符而不是字节的情况下使用PrintWriter类。
  17. *
  18. * @author Frank Yellin
  19. * @author Mark Reinhold
  20. * @since JDK1.0
  21. */
  22. public class PrintStream extends FilterOutputStream
  23. implements Appendable, Closeable

字段autoFlush,trouble,formatter,textOut,charOut,方法requireNonNull,toCharset

  1. // 是否自动刷新的标志
  2. private final boolean autoFlush;
  3. // 表示类内部是否报错的标志
  4. private boolean trouble = false;
  5. private Formatter formatter;
  6. /**
  7. * 同时跟踪文本输出流和字符输出流,
  8. * 以便在不刷新整个流的情况下刷新它们的缓冲区。
  9. */
  10. private BufferedWriter textOut;
  11. private OutputStreamWriter charOut;
  12. /**
  13. * 在这里显式声明requireNonNull,以避免在系统初始化期间加载java.util.Objects.requireNonNull。
  14. */
  15. private static <T> T requireNonNull(T obj, String message) {
  16. if (obj == null)
  17. throw new NullPointerException(message);
  18. return obj;
  19. }
  20. /**
  21. * 返回给定字符集名称的Charset对象。
  22. * @throws NullPointerException is csn is null
  23. * @throws UnsupportedEncodingException if the charset is not supported
  24. */
  25. private static Charset toCharset(String csn)
  26. throws UnsupportedEncodingException
  27. {
  28. requireNonNull(csn, "charsetName");
  29. try {
  30. // 调用Charset的方法
  31. return Charset.forName(csn);
  32. } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
  33. // UnsupportedEncodingException should be thrown
  34. throw new UnsupportedEncodingException(csn);
  35. }
  36. }

3个私有构造函数,7个公共构造函数

  1. /* 私有构造器 */
  2. private PrintStream(boolean autoFlush, OutputStream out) {
  3. // 以out作为底层的out
  4. super(out);
  5. // 设置自动刷新
  6. this.autoFlush = autoFlush;
  7. // 把自己,PrintStream作为参数,给OutputStreamWriter,然后赋值给charOut
  8. // charOut = new OutputStreamWriter(new PrintStream(out))
  9. this.charOut = new OutputStreamWriter(this);
  10. // 把charOut,OutputStreamWriter作为参数,给BufferedWriter,然后赋值给textOut
  11. // textOut = new BufferedWriter(new OutputStreamWriter(new PrintStream(out)))
  12. this.textOut = new BufferedWriter(charOut);
  13. }
  14. private PrintStream(boolean autoFlush, OutputStream out, Charset charset) {
  15. super(out);
  16. this.autoFlush = autoFlush;
  17. // charset作为参数,传递给OutputStreamWriter,charOut
  18. this.charOut = new OutputStreamWriter(this, charset);
  19. this.textOut = new BufferedWriter(charOut);
  20. }
  21. /* 私有构造函数的变体,以便在计算OutputStream实参之前验证给定的字符集名称。
  22. * 由构造函数使用,它创建一个带有字符集名称的FileOutputStream。
  23. */
  24. private PrintStream(boolean autoFlush, Charset charset, OutputStream out)
  25. throws UnsupportedEncodingException
  26. {
  27. this(autoFlush, out, charset);
  28. }
  29. /**
  30. * 创建一个新的打印流。此流不会自动刷新。
  31. *
  32. * @param out The output stream to which values and objects will be
  33. * printed
  34. *
  35. * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream)
  36. */
  37. public PrintStream(OutputStream out) {
  38. this(out, false);
  39. }
  40. /**
  41. * 创建一个新的打印流。
  42. *
  43. * @param out The output stream to which values and objects will be
  44. * printed
  45. * @param autoFlush A boolean; if true, the output buffer will be flushed
  46. * whenever a byte array is written, one of the
  47. * <code>println</code> methods is invoked, or a newline
  48. * character or byte (<code>'\n'</code>) is written
  49. *
  50. * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
  51. */
  52. public PrintStream(OutputStream out, boolean autoFlush) {
  53. this(autoFlush, requireNonNull(out, "Null output stream"));
  54. }
  55. /**
  56. * 创建一个新的打印流。
  57. *
  58. * @param out The output stream to which values and objects will be
  59. * printed
  60. * @param autoFlush A boolean; if true, the output buffer will be flushed
  61. * whenever a byte array is written, one of the
  62. * <code>println</code> methods is invoked, or a newline
  63. * character or byte (<code>'\n'</code>) is written
  64. * @param encoding The name of a supported
  65. * <a href="../lang/package-summary.html#charenc">
  66. * character encoding</a>
  67. *
  68. * @throws UnsupportedEncodingException
  69. * If the named encoding is not supported
  70. *
  71. * @since 1.4
  72. */
  73. public PrintStream(OutputStream out, boolean autoFlush, String encoding)
  74. throws UnsupportedEncodingException
  75. {
  76. this(autoFlush,
  77. requireNonNull(out, "Null output stream"),
  78. toCharset(encoding));
  79. }
  80. /**
  81. * 使用指定的文件名创建一个新的打印流,不需要自动刷新行。
  82. * 这个方便的构造函数创建必要的中间值OutputStreamWriter,
  83. * 它将使用Java虚拟机的这个实例的默认charset对字符进行编码。
  84. *
  85. * @param fileName
  86. * The name of the file to use as the destination of this print
  87. * stream. If the file exists, then it will be truncated to
  88. * zero size; otherwise, a new file will be created. The output
  89. * will be written to the file and is buffered.
  90. *
  91. * @throws FileNotFoundException
  92. * If the given file object does not denote an existing, writable
  93. * regular file and a new regular file of that name cannot be
  94. * created, or if some other error occurs while opening or
  95. * creating the file
  96. *
  97. * @throws SecurityException
  98. * If a security manager is present and {@link
  99. * SecurityManager#checkWrite checkWrite(fileName)} denies write
  100. * access to the file
  101. *
  102. * @since 1.5
  103. */
  104. public PrintStream(String fileName) throws FileNotFoundException {
  105. this(false, new FileOutputStream(fileName));
  106. }
  107. /**
  108. * 使用指定的文件名创建一个新的打印流,不需要自动刷新行。
  109. * 这个方便的构造函数创建必要的中间值OutputStreamWriter,
  110. * 它将使用提供的charset编码字符。
  111. *
  112. * @param fileName
  113. * The name of the file to use as the destination of this print
  114. * stream. If the file exists, then it will be truncated to
  115. * zero size; otherwise, a new file will be created. The output
  116. * will be written to the file and is buffered.
  117. *
  118. * @param csn
  119. * The name of a supported {@linkplain java.nio.charset.Charset
  120. * charset}
  121. *
  122. * @throws FileNotFoundException
  123. * If the given file object does not denote an existing, writable
  124. * regular file and a new regular file of that name cannot be
  125. * created, or if some other error occurs while opening or
  126. * creating the file
  127. *
  128. * @throws SecurityException
  129. * If a security manager is present and {@link
  130. * SecurityManager#checkWrite checkWrite(fileName)} denies write
  131. * access to the file
  132. *
  133. * @throws UnsupportedEncodingException
  134. * If the named charset is not supported
  135. *
  136. * @since 1.5
  137. */
  138. public PrintStream(String fileName, String csn)
  139. throws FileNotFoundException, UnsupportedEncodingException
  140. {
  141. // ensure charset is checked before the file is opened
  142. this(false, toCharset(csn), new FileOutputStream(fileName));
  143. }
  144. /**
  145. * 使用指定的文件创建一个新的打印流,不需要自动刷新行。
  146. * 这个方便的构造函数创建了必要的中间值OutputStreamWriter,
  147. * 它将使用Java虚拟机的这个实例的默认字符集对字符进行编码。
  148. *
  149. * @param file
  150. * The file to use as the destination of this print stream. If the
  151. * file exists, then it will be truncated to zero size; otherwise,
  152. * a new file will be created. The output will be written to the
  153. * file and is buffered.
  154. *
  155. * @throws FileNotFoundException
  156. * If the given file object does not denote an existing, writable
  157. * regular file and a new regular file of that name cannot be
  158. * created, or if some other error occurs while opening or
  159. * creating the file
  160. *
  161. * @throws SecurityException
  162. * If a security manager is present and {@link
  163. * SecurityManager#checkWrite checkWrite(file.getPath())}
  164. * denies write access to the file
  165. *
  166. * @since 1.5
  167. */
  168. public PrintStream(File file) throws FileNotFoundException {
  169. this(false, new FileOutputStream(file));
  170. }
  171. /**
  172. * 使用特定的文件和字符集创建一个新的打印流,不需要自动刷新行。
  173. * 这个方便的构造函数创建必要的中间OutputStreamWriter,
  174. * 它将使用提供的charset编码字符。
  175. *
  176. * @param file
  177. * The file to use as the destination of this print stream. If the
  178. * file exists, then it will be truncated to zero size; otherwise,
  179. * a new file will be created. The output will be written to the
  180. * file and is buffered.
  181. *
  182. * @param csn
  183. * The name of a supported {@linkplain java.nio.charset.Charset
  184. * charset}
  185. *
  186. * @throws FileNotFoundException
  187. * If the given file object does not denote an existing, writable
  188. * regular file and a new regular file of that name cannot be
  189. * created, or if some other error occurs while opening or
  190. * creating the file
  191. *
  192. * @throws SecurityException
  193. * If a security manager is present and {@link
  194. * SecurityManager#checkWrite checkWrite(file.getPath())}
  195. * denies write access to the file
  196. *
  197. * @throws UnsupportedEncodingException
  198. * If the named charset is not supported
  199. *
  200. * @since 1.5
  201. */
  202. public PrintStream(File file, String csn)
  203. throws FileNotFoundException, UnsupportedEncodingException
  204. {
  205. // ensure charset is checked before the file is opened
  206. this(false, toCharset(csn), new FileOutputStream(file));
  207. }

字段closing,方法ensureOpen,flush,close,checkError,setError,clearError

  1. /** 检查以确保流没有被关闭 */
  2. private void ensureOpen() throws IOException {
  3. if (out == null)
  4. throw new IOException("Stream closed");
  5. }
  6. /**
  7. * 刷新流。
  8. * 这是通过将任何缓冲的输出字节写入底层输出流,然后刷新该流来实现的。
  9. *
  10. * @see java.io.OutputStream#flush()
  11. */
  12. public void flush() {
  13. synchronized (this) {
  14. try {
  15. // flush底层的out
  16. ensureOpen();
  17. out.flush();
  18. }
  19. catch (IOException x) {
  20. trouble = true;
  21. }
  22. }
  23. }
  24. private boolean closing = false; /* 避免递归关闭 */
  25. /**
  26. * 关闭流。这是通过刷新流,然后关闭底层输出流来完成的。
  27. *
  28. * @see java.io.OutputStream#close()
  29. */
  30. public void close() {
  31. synchronized (this) {
  32. if (! closing) {
  33. closing = true;
  34. try {
  35. // 关闭textOut,意味着关闭charOut,同时再次关闭PrintStream,遇到closing,结束
  36. textOut.close();
  37. // 关闭底层out
  38. out.close();
  39. }
  40. catch (IOException x) {
  41. trouble = true;
  42. }
  43. // 设置3个out为null
  44. textOut = null;
  45. charOut = null;
  46. out = null;
  47. }
  48. }
  49. }
  50. /**
  51. * 刷新流并检查其错误状态。
  52. * 当底层输出流抛出除InterruptedIOException之外的IOException时,并且当调用setError方法时,内部错误状态设置为true。
  53. * 如果底层输出流上的操作抛出InterruptedIOException,则printstream会通过以下操作将异常转换回中断:
  54. * <pre>
  55. * Thread.currentThread().interrupt();
  56. * </pre>
  57. * 或者是等价的。
  58. *
  59. * @return <code>true</code> if and only if this stream has encountered an
  60. * <code>IOException</code> other than
  61. * <code>InterruptedIOException</code>, or the
  62. * <code>setError</code> method has been invoked
  63. */
  64. public boolean checkError() {
  65. if (out != null)
  66. flush();
  67. if (out instanceof java.io.PrintStream) {
  68. PrintStream ps = (PrintStream) out;
  69. return ps.checkError();
  70. }
  71. return trouble;
  72. }
  73. /**
  74. * 将流的错误状态设置为true。
  75. *
  76. * <p> 这个方法将导致随后对checkError()的调用返回true,直到调用clearError()。
  77. *
  78. * @since JDK1.1
  79. */
  80. protected void setError() {
  81. trouble = true;
  82. }
  83. /**
  84. * 清除此流的内部错误状态。
  85. *
  86. * <p> 此方法将导致后续对checkError()的调用返回false,直到另一个写操作失败并调用setError()。
  87. *
  88. * @since 1.6
  89. */
  90. protected void clearError() {
  91. trouble = false;
  92. }

2个公共write方法,2个私有write方法,newLine方法

  1. /*
  2. * Exception-catching, synchronized output operations,
  3. * which also implement the write() methods of OutputStream
  4. */
  5. /**
  6. * 将指定的字节写入此流。
  7. * 如果字节是换行符,并且启用了自动刷新,那么将调用flush方法。
  8. *
  9. * <p> 注意,字节是按给定的方式写入的;
  10. * 要编写一个根据平台的默认字符编码进行翻译的字符,可以使用print(char)或println(char)方法。
  11. *
  12. * @param b The byte to be written
  13. * @see #print(char)
  14. * @see #println(char)
  15. */
  16. public void write(int b) {
  17. try {
  18. synchronized (this) {
  19. ensureOpen();
  20. // 底层out写入b
  21. out.write(b);
  22. // 对out进行刷新
  23. if ((b == '\n') && autoFlush)
  24. out.flush();
  25. }
  26. }
  27. catch (InterruptedIOException x) {
  28. Thread.currentThread().interrupt();
  29. }
  30. catch (IOException x) {
  31. trouble = true;
  32. }
  33. }
  34. /**
  35. * 将指定字节数组中的len字节从off开始写入到此流。
  36. * 如果启用了自动刷新,那么将调用flush方法。
  37. *
  38. * <p> 注意,字节将按给定的方式写入;
  39. * 要编写将根据平台的默认字符编码进行翻译的字符,请使用print(char)或println(char)方法。
  40. *
  41. * @param buf A byte array
  42. * @param off Offset from which to start taking bytes
  43. * @param len Number of bytes to write
  44. */
  45. public void write(byte buf[], int off, int len) {
  46. try {
  47. synchronized (this) {
  48. ensureOpen();
  49. out.write(buf, off, len);
  50. if (autoFlush)
  51. out.flush();
  52. }
  53. }
  54. catch (InterruptedIOException x) {
  55. Thread.currentThread().interrupt();
  56. }
  57. catch (IOException x) {
  58. trouble = true;
  59. }
  60. }
  61. /*
  62. * 文本和字符输出流上的以下私有方法总是刷新流缓冲区,
  63. * 因此对底层字节流的写操作与原始PrintStream一样及时。
  64. */
  65. private void write(char buf[]) {
  66. try {
  67. synchronized (this) {
  68. ensureOpen();
  69. // textOut写入char数组,意味着BufferedWriter,OutputStreamWriter,PrintStream,out都写入了这个char数组
  70. textOut.write(buf);
  71. // 都刷新缓存
  72. textOut.flushBuffer();
  73. charOut.flushBuffer();
  74. if (autoFlush) {
  75. for (int i = 0; i < buf.length; i++)
  76. // 如果有一个char为\n,刷新底层流
  77. if (buf[i] == '\n')
  78. out.flush();
  79. }
  80. }
  81. }
  82. catch (InterruptedIOException x) {
  83. Thread.currentThread().interrupt();
  84. }
  85. catch (IOException x) {
  86. trouble = true;
  87. }
  88. }
  89. private void write(String s) {
  90. try {
  91. synchronized (this) {
  92. ensureOpen();
  93. // 写入textOut
  94. textOut.write(s);
  95. textOut.flushBuffer();
  96. charOut.flushBuffer();
  97. if (autoFlush && (s.indexOf('\n') >= 0))
  98. out.flush();
  99. }
  100. }
  101. catch (InterruptedIOException x) {
  102. Thread.currentThread().interrupt();
  103. }
  104. catch (IOException x) {
  105. trouble = true;
  106. }
  107. }
  108. private void newLine() {
  109. try {
  110. synchronized (this) {
  111. ensureOpen();
  112. // 写入行分隔符
  113. textOut.newLine();
  114. textOut.flushBuffer();
  115. charOut.flushBuffer();
  116. if (autoFlush)
  117. out.flush();
  118. }
  119. }
  120. catch (InterruptedIOException x) {
  121. Thread.currentThread().interrupt();
  122. }
  123. catch (IOException x) {
  124. trouble = true;
  125. }
  126. }

9个print方法

  1. /* 不终止行的方法 */
  2. /**
  3. * 输出一个布尔值。java.lang.String.valueOf(boolean)生成的字符串根据
  4. * 平台的默认字符编码被转换成字节,并且这些字节完全按照write(int)方法的方式被写入。
  5. *
  6. * @param b The <code>boolean</code> to be printed
  7. */
  8. public void print(boolean b) {
  9. write(b ? "true" : "false");
  10. }
  11. /**
  12. * 输出一个字符。
  13. * 根据平台的默认字符编码,字符被转换成一个或多个字节码,这些字节码完全按照write(int)方法的方式写入。
  14. *
  15. * @param c The <code>char</code> to be printed
  16. */
  17. public void print(char c) {
  18. write(String.valueOf(c));
  19. }
  20. /**
  21. * Prints an integer. The string produced by <code>{@link
  22. * java.lang.String#valueOf(int)}</code> is translated into bytes
  23. * according to the platform's default character encoding, and these bytes
  24. * are written in exactly the manner of the
  25. * <code>{@link #write(int)}</code> method.
  26. *
  27. * @param i The <code>int</code> to be printed
  28. * @see java.lang.Integer#toString(int)
  29. */
  30. public void print(int i) {
  31. write(String.valueOf(i));
  32. }
  33. /**
  34. * Prints a long integer. The string produced by <code>{@link
  35. * java.lang.String#valueOf(long)}</code> is translated into bytes
  36. * according to the platform's default character encoding, and these bytes
  37. * are written in exactly the manner of the
  38. * <code>{@link #write(int)}</code> method.
  39. *
  40. * @param l The <code>long</code> to be printed
  41. * @see java.lang.Long#toString(long)
  42. */
  43. public void print(long l) {
  44. write(String.valueOf(l));
  45. }
  46. /**
  47. * Prints a floating-point number. The string produced by <code>{@link
  48. * java.lang.String#valueOf(float)}</code> is translated into bytes
  49. * according to the platform's default character encoding, and these bytes
  50. * are written in exactly the manner of the
  51. * <code>{@link #write(int)}</code> method.
  52. *
  53. * @param f The <code>float</code> to be printed
  54. * @see java.lang.Float#toString(float)
  55. */
  56. public void print(float f) {
  57. write(String.valueOf(f));
  58. }
  59. /**
  60. * Prints a double-precision floating-point number. The string produced by
  61. * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
  62. * bytes according to the platform's default character encoding, and these
  63. * bytes are written in exactly the manner of the <code>{@link
  64. * #write(int)}</code> method.
  65. *
  66. * @param d The <code>double</code> to be printed
  67. * @see java.lang.Double#toString(double)
  68. */
  69. public void print(double d) {
  70. write(String.valueOf(d));
  71. }
  72. /**
  73. * Prints an array of characters. The characters are converted into bytes
  74. * according to the platform's default character encoding, and these bytes
  75. * are written in exactly the manner of the
  76. * <code>{@link #write(int)}</code> method.
  77. *
  78. * @param s The array of chars to be printed
  79. *
  80. * @throws NullPointerException If <code>s</code> is <code>null</code>
  81. */
  82. public void print(char s[]) {
  83. write(s);
  84. }
  85. /**
  86. * Prints a string. If the argument is <code>null</code> then the string
  87. * <code>"null"</code> is printed. Otherwise, the string's characters are
  88. * converted into bytes according to the platform's default character
  89. * encoding, and these bytes are written in exactly the manner of the
  90. * <code>{@link #write(int)}</code> method.
  91. *
  92. * @param s The <code>String</code> to be printed
  93. */
  94. public void print(String s) {
  95. if (s == null) {
  96. s = "null";
  97. }
  98. write(s);
  99. }
  100. /**
  101. * Prints an object. The string produced by the <code>{@link
  102. * java.lang.String#valueOf(Object)}</code> method is translated into bytes
  103. * according to the platform's default character encoding, and these bytes
  104. * are written in exactly the manner of the
  105. * <code>{@link #write(int)}</code> method.
  106. *
  107. * @param obj The <code>Object</code> to be printed
  108. * @see java.lang.Object#toString()
  109. */
  110. public void print(Object obj) {
  111. write(String.valueOf(obj));
  112. }

10个println方法

  1. /* 终止行的方法 */
  2. /**
  3. * 通过写入行分隔符字符串终止当前行。
  4. * 线分隔符字符串由系统属性line.separator定义。
  5. * 而不一定是单个换行字符 ('\n')。
  6. */
  7. public void println() {
  8. newLine();
  9. }
  10. /**
  11. * 输出一个布尔值,然后终止行。
  12. * 这个方法的行为就像它调用print(boolean)然后调用println()一样。
  13. *
  14. * @param x The <code>boolean</code> to be printed
  15. */
  16. public void println(boolean x) {
  17. synchronized (this) {
  18. print(x);
  19. newLine();
  20. }
  21. }
  22. /**
  23. * Prints a character and then terminate the line. This method behaves as
  24. * though it invokes <code>{@link #print(char)}</code> and then
  25. * <code>{@link #println()}</code>.
  26. *
  27. * @param x The <code>char</code> to be printed.
  28. */
  29. public void println(char x) {
  30. synchronized (this) {
  31. print(x);
  32. newLine();
  33. }
  34. }
  35. /**
  36. * Prints an integer and then terminate the line. This method behaves as
  37. * though it invokes <code>{@link #print(int)}</code> and then
  38. * <code>{@link #println()}</code>.
  39. *
  40. * @param x The <code>int</code> to be printed.
  41. */
  42. public void println(int x) {
  43. synchronized (this) {
  44. print(x);
  45. newLine();
  46. }
  47. }
  48. /**
  49. * Prints a long and then terminate the line. This method behaves as
  50. * though it invokes <code>{@link #print(long)}</code> and then
  51. * <code>{@link #println()}</code>.
  52. *
  53. * @param x a The <code>long</code> to be printed.
  54. */
  55. public void println(long x) {
  56. synchronized (this) {
  57. print(x);
  58. newLine();
  59. }
  60. }
  61. /**
  62. * Prints a float and then terminate the line. This method behaves as
  63. * though it invokes <code>{@link #print(float)}</code> and then
  64. * <code>{@link #println()}</code>.
  65. *
  66. * @param x The <code>float</code> to be printed.
  67. */
  68. public void println(float x) {
  69. synchronized (this) {
  70. print(x);
  71. newLine();
  72. }
  73. }
  74. /**
  75. * Prints a double and then terminate the line. This method behaves as
  76. * though it invokes <code>{@link #print(double)}</code> and then
  77. * <code>{@link #println()}</code>.
  78. *
  79. * @param x The <code>double</code> to be printed.
  80. */
  81. public void println(double x) {
  82. synchronized (this) {
  83. print(x);
  84. newLine();
  85. }
  86. }
  87. /**
  88. * Prints an array of characters and then terminate the line. This method
  89. * behaves as though it invokes <code>{@link #print(char[])}</code> and
  90. * then <code>{@link #println()}</code>.
  91. *
  92. * @param x an array of chars to print.
  93. */
  94. public void println(char x[]) {
  95. synchronized (this) {
  96. print(x);
  97. newLine();
  98. }
  99. }
  100. /**
  101. * Prints a String and then terminate the line. This method behaves as
  102. * though it invokes <code>{@link #print(String)}</code> and then
  103. * <code>{@link #println()}</code>.
  104. *
  105. * @param x The <code>String</code> to be printed.
  106. */
  107. public void println(String x) {
  108. synchronized (this) {
  109. print(x);
  110. newLine();
  111. }
  112. }
  113. /**
  114. * Prints an Object and then terminate the line. This method calls
  115. * at first String.valueOf(x) to get the printed object's string value,
  116. * then behaves as
  117. * though it invokes <code>{@link #print(String)}</code> and then
  118. * <code>{@link #println()}</code>.
  119. *
  120. * @param x The <code>Object</code> to be printed.
  121. */
  122. public void println(Object x) {
  123. String s = String.valueOf(x);
  124. synchronized (this) {
  125. print(s);
  126. newLine();
  127. }
  128. }

2个printf方法,2个format方法,3个append方法

  1. /**
  2. * 使用指定的格式字符串和参数将格式化字符串写入输出流的方便方法。
  3. *
  4. * <p> 调用out.printf(format,args)形式的这个方法的行为,与调用下面的方式完全相同。
  5. *
  6. * <pre>
  7. * out.format(format, args) </pre>
  8. *
  9. * @param format
  10. * A format string as described in <a
  11. * href="../util/Formatter.html#syntax">Format string syntax</a>
  12. *
  13. * @param args
  14. * Arguments referenced by the format specifiers in the format
  15. * string. If there are more arguments than format specifiers, the
  16. * extra arguments are ignored. The number of arguments is
  17. * variable and may be zero. The maximum number of arguments is
  18. * limited by the maximum dimension of a Java array as defined by
  19. * <cite>The Java&trade; Virtual Machine Specification</cite>.
  20. * The behaviour on a
  21. * <tt>null</tt> argument depends on the <a
  22. * href="../util/Formatter.html#syntax">conversion</a>.
  23. *
  24. * @throws java.util.IllegalFormatException
  25. * If a format string contains an illegal syntax, a format
  26. * specifier that is incompatible with the given arguments,
  27. * insufficient arguments given the format string, or other
  28. * illegal conditions. For specification of all possible
  29. * formatting errors, see the <a
  30. * href="../util/Formatter.html#detail">Details</a> section of the
  31. * formatter class specification.
  32. *
  33. * @throws NullPointerException
  34. * If the <tt>format</tt> is <tt>null</tt>
  35. *
  36. * @return This output stream
  37. *
  38. * @since 1.5
  39. */
  40. public PrintStream printf(String format, Object ... args) {
  41. return format(format, args);
  42. }
  43. /**
  44. * 使用指定的格式字符串和参数将格式化字符串写入输出流的方便方法。
  45. *
  46. * <p> 调用out.printf(l, format,args)形式的这个方法的行为,与调用下面的方式完全相同。
  47. *
  48. * <pre>
  49. * out.format(l, format, args) </pre>
  50. *
  51. * @param l
  52. * The {@linkplain java.util.Locale locale} to apply during
  53. * formatting. If <tt>l</tt> is <tt>null</tt> then no localization
  54. * is applied.
  55. *
  56. * @param format
  57. * A format string as described in <a
  58. * href="../util/Formatter.html#syntax">Format string syntax</a>
  59. *
  60. * @param args
  61. * Arguments referenced by the format specifiers in the format
  62. * string. If there are more arguments than format specifiers, the
  63. * extra arguments are ignored. The number of arguments is
  64. * variable and may be zero. The maximum number of arguments is
  65. * limited by the maximum dimension of a Java array as defined by
  66. * <cite>The Java&trade; Virtual Machine Specification</cite>.
  67. * The behaviour on a
  68. * <tt>null</tt> argument depends on the <a
  69. * href="../util/Formatter.html#syntax">conversion</a>.
  70. *
  71. * @throws java.util.IllegalFormatException
  72. * If a format string contains an illegal syntax, a format
  73. * specifier that is incompatible with the given arguments,
  74. * insufficient arguments given the format string, or other
  75. * illegal conditions. For specification of all possible
  76. * formatting errors, see the <a
  77. * href="../util/Formatter.html#detail">Details</a> section of the
  78. * formatter class specification.
  79. *
  80. * @throws NullPointerException
  81. * If the <tt>format</tt> is <tt>null</tt>
  82. *
  83. * @return This output stream
  84. *
  85. * @since 1.5
  86. */
  87. public PrintStream printf(Locale l, String format, Object ... args) {
  88. return format(l, format, args);
  89. }
  90. /**
  91. * 使用指定格式字符串和参数将格式化字符串写入输出流。
  92. *
  93. * <p> 始终使用的语言环境是Locale.getDefault()返回的语言环境,
  94. * 而不考虑之前对该对象的其他格式化方法的调用。
  95. *
  96. * @param format
  97. * A format string as described in <a
  98. * href="../util/Formatter.html#syntax">Format string syntax</a>
  99. *
  100. * @param args
  101. * Arguments referenced by the format specifiers in the format
  102. * string. If there are more arguments than format specifiers, the
  103. * extra arguments are ignored. The number of arguments is
  104. * variable and may be zero. The maximum number of arguments is
  105. * limited by the maximum dimension of a Java array as defined by
  106. * <cite>The Java&trade; Virtual Machine Specification</cite>.
  107. * The behaviour on a
  108. * <tt>null</tt> argument depends on the <a
  109. * href="../util/Formatter.html#syntax">conversion</a>.
  110. *
  111. * @throws java.util.IllegalFormatException
  112. * If a format string contains an illegal syntax, a format
  113. * specifier that is incompatible with the given arguments,
  114. * insufficient arguments given the format string, or other
  115. * illegal conditions. For specification of all possible
  116. * formatting errors, see the <a
  117. * href="../util/Formatter.html#detail">Details</a> section of the
  118. * formatter class specification.
  119. *
  120. * @throws NullPointerException
  121. * If the <tt>format</tt> is <tt>null</tt>
  122. *
  123. * @return This output stream
  124. *
  125. * @since 1.5
  126. */
  127. public PrintStream format(String format, Object ... args) {
  128. try {
  129. synchronized (this) {
  130. ensureOpen();
  131. if ((formatter == null)
  132. || (formatter.locale() != Locale.getDefault()))
  133. // 以自己作为终点,创建一个Formatter
  134. formatter = new Formatter((Appendable) this);
  135. // 使用特定的区域设置、格式字符串和参数将格式化字符串写入此对象的目标。
  136. formatter.format(Locale.getDefault(), format, args);
  137. }
  138. } catch (InterruptedIOException x) {
  139. Thread.currentThread().interrupt();
  140. } catch (IOException x) {
  141. trouble = true;
  142. }
  143. return this;
  144. }
  145. /**
  146. * 使用指定格式字符串和参数将格式化字符串写入输出流。
  147. *
  148. * @param l
  149. * The {@linkplain java.util.Locale locale} to apply during
  150. * formatting. If <tt>l</tt> is <tt>null</tt> then no localization
  151. * is applied.
  152. *
  153. * @param format
  154. * A format string as described in <a
  155. * href="../util/Formatter.html#syntax">Format string syntax</a>
  156. *
  157. * @param args
  158. * Arguments referenced by the format specifiers in the format
  159. * string. If there are more arguments than format specifiers, the
  160. * extra arguments are ignored. The number of arguments is
  161. * variable and may be zero. The maximum number of arguments is
  162. * limited by the maximum dimension of a Java array as defined by
  163. * <cite>The Java&trade; Virtual Machine Specification</cite>.
  164. * The behaviour on a
  165. * <tt>null</tt> argument depends on the <a
  166. * href="../util/Formatter.html#syntax">conversion</a>.
  167. *
  168. * @throws java.util.IllegalFormatException
  169. * If a format string contains an illegal syntax, a format
  170. * specifier that is incompatible with the given arguments,
  171. * insufficient arguments given the format string, or other
  172. * illegal conditions. For specification of all possible
  173. * formatting errors, see the <a
  174. * href="../util/Formatter.html#detail">Details</a> section of the
  175. * formatter class specification.
  176. *
  177. * @throws NullPointerException
  178. * If the <tt>format</tt> is <tt>null</tt>
  179. *
  180. * @return This output stream
  181. *
  182. * @since 1.5
  183. */
  184. public PrintStream format(Locale l, String format, Object ... args) {
  185. try {
  186. synchronized (this) {
  187. ensureOpen();
  188. if ((formatter == null)
  189. || (formatter.locale() != l))
  190. formatter = new Formatter(this, l);
  191. formatter.format(l, format, args);
  192. }
  193. } catch (InterruptedIOException x) {
  194. Thread.currentThread().interrupt();
  195. } catch (IOException x) {
  196. trouble = true;
  197. }
  198. return this;
  199. }
  200. /**
  201. * 将指定的字符序列追加到此输出流。
  202. *
  203. * <p> 对out.append(csq)这个方法的调用行为与下面完全相同
  204. *
  205. * <pre>
  206. * out.print(csq.toString()) </pre>
  207. *
  208. * <p> 根据字符序列csq的toString规范,整个序列可能不会被附加。
  209. * 例如,调用一个字符缓冲区的toString方法将返回一个子序列,其内容取决于缓冲区的位置和限制。
  210. *
  211. * @param csq
  212. * The character sequence to append. If <tt>csq</tt> is
  213. * <tt>null</tt>, then the four characters <tt>"null"</tt> are
  214. * appended to this output stream.
  215. *
  216. * @return This output stream
  217. *
  218. * @since 1.5
  219. */
  220. public PrintStream append(CharSequence csq) {
  221. if (csq == null)
  222. print("null");
  223. else
  224. print(csq.toString());
  225. return this;
  226. }
  227. /**
  228. * Appends a subsequence of the specified character sequence to this output
  229. * stream.
  230. *
  231. * <p> An invocation of this method of the form <tt>out.append(csq, start,
  232. * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
  233. * exactly the same way as the invocation
  234. *
  235. * <pre>
  236. * out.print(csq.subSequence(start, end).toString()) </pre>
  237. *
  238. * @param csq
  239. * The character sequence from which a subsequence will be
  240. * appended. If <tt>csq</tt> is <tt>null</tt>, then characters
  241. * will be appended as if <tt>csq</tt> contained the four
  242. * characters <tt>"null"</tt>.
  243. *
  244. * @param start
  245. * The index of the first character in the subsequence
  246. *
  247. * @param end
  248. * The index of the character following the last character in the
  249. * subsequence
  250. *
  251. * @return This output stream
  252. *
  253. * @throws IndexOutOfBoundsException
  254. * If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
  255. * is greater than <tt>end</tt>, or <tt>end</tt> is greater than
  256. * <tt>csq.length()</tt>
  257. *
  258. * @since 1.5
  259. */
  260. public PrintStream append(CharSequence csq, int start, int end) {
  261. CharSequence cs = (csq == null ? "null" : csq);
  262. write(cs.subSequence(start, end).toString());
  263. return this;
  264. }
  265. /**
  266. * Appends the specified character to this output stream.
  267. *
  268. * <p> An invocation of this method of the form <tt>out.append(c)</tt>
  269. * behaves in exactly the same way as the invocation
  270. *
  271. * <pre>
  272. * out.print(c) </pre>
  273. *
  274. * @param c
  275. * The 16-bit character to append
  276. *
  277. * @return This output stream
  278. *
  279. * @since 1.5
  280. */
  281. public PrintStream append(char c) {
  282. print(c);
  283. return this;
  284. }

 

原文链接:https://blog.csdn.net/xushiyu1996818/article/details/109724299



所属网站分类: 技术文章 > 博客

作者:javagogogo

链接:http://www.javaheidong.com/blog/article/919/97afd938ecc390fa7855/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

16 0
收藏该文
已收藏

评论内容:(最多支持255个字符)