问题描述
我已经在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)
。
问题:
-
如果没有采用PrintStream的PrintWriter构造器,那么我们怎么能像我在下面写的那样写一个陈述?
-
如果没有采用BufferedWriter的PrintWriter构造器,那么我们怎么能像我在下面写的那样写一个陈述?
PrintWriter writer1 = new PrintWriter(System.out); PrintWriter writer2; writer2 = new PrintWriter(new BufferedWriter(new FileWriter(new File(outdir,reportFileName))));
谢谢。
解决方法
这是可能的,因为PrintStream继承自FilterOutputStream,后者又继承自OutputStream。
BufferedWriter继承自Writer。
Writer和OutputStream是PrintWriter构造函数中的可能变量。最重要的话题是继承。有一个good post。