View Javadoc

    1   package net.sourceforge.pmd.util;
    2
    3   import java.io.BufferedWriter;
    4   import java.io.Closeable;
    5   import java.io.FileWriter;
    6   import java.io.IOException;
    7   import java.io.OutputStreamWriter;
    8   import java.io.Writer;
    9
    10  /**
    11   *
    12   * @author Brian Remedios
    13   */
    14  public class IOUtil {
    15
    16  	private IOUtil() {}
    17
    18  	/**
    19  	 * Convenience methods to close any stream, reader, or writer. Ignores
    20  	 * null values.
    21  	 *
    22  	 * @param closeable
    23  	 */
    24      public static void closeQuietly(Closeable closeable) {
    25      	if (closeable == null) return;
    26      	try {
    27      		closeable.close();
    28      	} catch (IOException ex) {
    29      		// ignore
    30      	}
    31      }
    32
    33      public static Writer createWriter() {
    34      	return new OutputStreamWriter(System.out);
    35      }
    36
    37      public static Writer createWriter(String reportFile) {
    38      	try {
    39      		return StringUtil.isEmpty(reportFile) ? createWriter()  : new BufferedWriter(new FileWriter(reportFile));
    40      	} catch (IOException e) {
    41      		throw new IllegalArgumentException(e);
    42      	}
    43      }
    44  }