View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.cpd;
5   
6   import java.io.*;
7   
8   /**
9    * @author Philippe T'Seyen
10   */
11  public class FileReporter {
12      private File reportFile;
13      private String encoding;
14  
15      public FileReporter(String encoding) {
16          this(null, encoding);
17      }
18  
19      public FileReporter(File reportFile) {
20          this(reportFile, System.getProperty("file.encoding"));
21      }
22  
23      public FileReporter(File reportFile, String encoding) {
24          this.reportFile = reportFile;
25          this.encoding = encoding;
26      }
27  
28      public void report(String content) throws ReportException {
29          try {
30              Writer writer = null;
31              try {
32              	OutputStream outputStream;
33              	if (reportFile == null) {
34              		outputStream = System.out;
35              	} else {
36              		outputStream = new FileOutputStream(reportFile);
37              	}
38                  writer = new BufferedWriter(new OutputStreamWriter(outputStream, encoding));
39                  writer.write(content);
40              } finally {
41                  if (writer != null) writer.close();
42              }
43          } catch (IOException ioe) {
44              throw new ReportException(ioe);
45          }
46      }
47  }