PrintWriter构造函数

问题描述

我已经在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))));
    

谢谢。

解决方法

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

enter image description here

BufferedWriter继承自Writer。

enter image description here

Writer和OutputStream是PrintWriter构造函数中的可能变量。最重要的话题是继承。有一个good post

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...