01 /**
02 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03 */
04 package net.sourceforge.pmd.cpd;
05
06 import java.io.*;
07
08 /**
09 * @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) {
42 writer.close();
43 }
44 }
45 } catch (IOException ioe) {
46 throw new ReportException(ioe);
47 }
48 }
49 }
|