jdk源码解析--printWriter类
Posted 我的IT技术路
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdk源码解析--printWriter类相关的知识,希望对你有一定的参考价值。
在之前,我们介绍了字符输出流的抽象基类,本节我们看下打印字符输出流。这是一个装饰器封装的底层流,装饰之后的打印流拥有print/println的功能,和write相比,它可以输出一行,自动添加换行符等功能。下面我们通过一个简单的demo看下这个流的使用方法。
1. import java.io.IOException;
2. import java.io.PrintWriter;
3. import java.io.StringWriter;
4.
5. public class PrintWriterTest {
6.
7. public static void main(String[] args) throws IOException {
8. try (StringWriter stringWriter = new StringWriter();//传入字符流
9. PrintWriter p = new PrintWriter(stringWriter)){//将字符流装饰成打印流
10. p.println(1);
11. p.println(false);
12. p.write("123");
13. System.out.println(stringWriter.toString());
14. }
15. }
16. }
我们将1,false,123都打印到字符流中,通过字符流的toString方法将传入进去的数据打印出来。看下最后的结果如下:
看完上面的demo之后,我们看下具体的实现类图:
1. public class PrintWriter extends Writer {
2. protected Writer out;//传入的输出流
3. private final boolean autoFlush;//是否自动刷新
4. private boolean trouble = false;//流是否是错误状态
5. private Formatter formatter;//格式化
6. private PrintStream psOut = null;//底层的字节打印流
7. private final String lineSeparator;//换行符
8. public PrintWriter (Writer out) {//构造函数系列
9. this(out, false);
10. }
11. public PrintWriter(Writer out,
12. boolean autoFlush) {
13. super(out);
14. this.out = out;
15. this.autoFlush = autoFlush;
16. lineSeparator = java.security.AccessController.doPrivileged(
17. new sun.security.action.GetPropertyAction("line.separator"));
18. }
19. public PrintWriter(OutputStream out) {
20. this(out, false);
21. }
22. public PrintWriter(OutputStream out, boolean autoFlush) {
23. this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
24.
25. // save print stream for error propagation
26. if (out instanceof java.io.PrintStream) {
27. psOut = (PrintStream) out;
28. }
29. }
30. public PrintWriter(String fileName) throws FileNotFoundException {
31. this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
32. false);
33. }
34. private PrintWriter(Charset charset, File file)
35. throws FileNotFoundException
36. {
37. this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)),
38. false);
39. }
40. public PrintWriter(String fileName, String csn)
41. throws FileNotFoundException, UnsupportedEncodingException
42. {
43. this(toCharset(csn), new File(fileName));
44. }
45. public PrintWriter(File file) throws FileNotFoundException {
46. this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
47. false);
48. }
49. public PrintWriter(File file, String csn)
50. throws FileNotFoundException, UnsupportedEncodingException
51. {
52. this(toCharset(csn), file);
53. }
54.
55. }
Flush:刷新
1. public void flush() {
2. try {
3. synchronized (lock) {//加锁
4. ensureOpen();//确保流没有关闭
5. out.flush();//刷新
6. }
7. }
8. catch (IOException x) {
9. trouble = true;//设置流异常
10. }
11. }
12. private void ensureOpen() throws IOException {
13. if (out == null)
14. throw new IOException("Stream closed");
15. }
Close:
1. public void close() {
2. try {
3. synchronized (lock) {//加锁
4. if (out == null)
5. return;
6. out.close();//关闭
7. out = null;
8. }
9. }
10. catch (IOException x) {
11. trouble = true;//设置异常
12. }
13. }
Write:写入的实现是调用底层的实现
1. public void write(int c) {
2. try {
3. synchronized (lock) {//加锁
4. ensureOpen();
5. out.write(c);//写入数据
6. }
7. }
8. catch (InterruptedIOException x) {
9. Thread.currentThread().interrupt();
10. }
11. catch (IOException x) {
12. trouble = true;
13. }
14. }
15. public void write(char buf[], int off, int len) {
16. try {
17. synchronized (lock) {
18. ensureOpen();
19. out.write(buf, off, len);//写入数据
20. }
21. }
22. catch (InterruptedIOException x) {
23. Thread.currentThread().interrupt();
24. }
25. catch (IOException x) {
26. trouble = true;
27. }
28. }
29. public void write(String s, int off, int len) {
30. try {
31. synchronized (lock) {
32. ensureOpen();
33. out.write(s, off, len);
34. }
35. }
36. catch (InterruptedIOException x) {
37. Thread.currentThread().interrupt();
38. }
39. catch (IOException x) {
40. trouble = true;
41. }
42. }
Print和println:
1. public void print(String s) {
2. if (s == null) {
3. s = "null";
4. }
5. write(s);//写入数据
6. }
7. public void println() {
8. newLine();//写入新的行
9. }
10. private void newLine() {
11. try {
12. synchronized (lock) {
13. ensureOpen();//确保写入
14. out.write(lineSeparator);//写入换行符
15. if (autoFlush)//自动刷新
16. out.flush();
17. }
18. }
19. catch (InterruptedIOException x) {
20. Thread.currentThread().interrupt();
21. }
22. catch (IOException x) {
23. trouble = true;
24. }
25. }
Append:追加方法
1. public PrintWriter append(CharSequence csq) {
2. if (csq == null)
3. write("null");//如果是null,写入null
4. else
5. write(csq.toString());//写入字符串序列
6. return this;
7. }
这个类和printStream有着相似的功能。不过是字符类型的。这个类的解析就到这里。
以上是关于jdk源码解析--printWriter类的主要内容,如果未能解决你的问题,请参考以下文章