PrintWriter构造函数

huangapple go评论62阅读模式
英文:

PrintWriter constructors

问题

我已经列出了 PrintWriter 类的 8 个构造函数。
PrintWriter(File file)PrintWriter(File file, String csn)PrintWriter(OutputStream out)
PrintWriter(OutputStream out, boolean autoFlush)PrintWriter(String fileName)
PrintWriter(String fileName, String csn)PrintWriter(Writer out)
PrintWriter(Writer out, boolean autoFlush)

问题:

  1. 如果没有接受 PrintStream 的 PrintWriter 构造函数,那么我们怎么能够写出像我下面写的语句呢?
  2. 如果没有接受 BufferedWriter 的 PrintWriter 构造函数,那么我们怎么能够写出像我下面写的语句呢?
PrintWriter writer1 = new PrintWriter(System.out);

PrintWriter writer2;
writer2 = new PrintWriter(new BufferedWriter(new FileWriter(new File(outdir, reportFileName))));

提前感谢您的回答。

英文:

I've listed below the 8 constructors of PrintWriter class.
PrintWriter(File file), PrintWriter(File file, String csn), PrintWriter(OutputStream out),
PrintWriter(OutputStream out, boolean autoFlush), PrintWriter(String fileName),
PrintWriter(String fileName, String csn), PrintWriter(Writer out),
PrintWriter(Writer out, boolean autoFlush).

Questions:

  1. If there's no PrintWriter contructor that takes PrintStream, then how come we can write a statment like I've written below?

  2. If there's no PrintWriter contructor that takes BufferedWriter, then how come we can write a statment like I've written below?

          PrintWriter writer1 = new PrintWriter(System.out);
    
          PrintWriter writer2;
          writer2 = new PrintWriter(new BufferedWriter(new FileWriter(new File(outdir, reportFileName))));
    

Thanks in advance.

答案1

得分: 2

这是可能的,因为PrintStream继承自FilterOutputStream,而后者再次继承自OutputStream。

PrintWriter构造函数

一个BufferedWriter继承自Writer。

PrintWriter构造函数

Writer和OutputStream是PrintWriter构造函数中可能的变量。我们在这里讨论的重要主题是继承。这有一个很好的帖子来解释这个。

英文:

This is possible because PrintStream inherits from FilterOutputStream and the latter again from OutputStream.

PrintWriter构造函数

A BufferedWriter inherits from Writer.

PrintWriter构造函数

Writer and OutputStream are possible variables in the PrintWriter constructors. The big topic, what we are talking about here, is inheritance. There is a good post for this.

huangapple
  • 本文由 发表于 2020年9月22日 03:52:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63999111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定