/**
 * Utility class to open PrintWriters and BufferedReaders
 */
import java.io.*;

public class FileUtils
{
    public static final String DEFAULT_ENCODING = "UTF-8";
    
    /**
     * Open a "UTF-8" PrintWriter using fileName, overwrite
     */
    public static PrintWriter openPrintWriter(String fileName)
        throws UnsupportedEncodingException, FileNotFoundException
    { 
        PrintWriter rval = openPrintWriter(fileName, false);
        return rval;
    }  
    
    /**
     * Open a "UTF-8" PrintWriter using File object, overwrite
     */
    public static PrintWriter openPrintWriter(File aFile)
        throws UnsupportedEncodingException, FileNotFoundException
    {   
        PrintWriter rval = openPrintWriter(aFile.getAbsolutePath(), false);
        return rval;
    }
    
    /**
     * Open a "UTF-8" PrintWriter using fileName, and value of boolean append (overwrite if false)
     */
    public static PrintWriter openPrintWriter(String fileName, boolean append)
        throws UnsupportedEncodingException, FileNotFoundException
    { 
        PrintWriter rval = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, append), DEFAULT_ENCODING)));
        return rval;
    }
    
    /**
     * Open a "UTF-8" PrintWriter using File object, and value of boolean append (overwrite if false)
     */    
    public static PrintWriter openPrintWriter(File theFile, boolean append)
        throws UnsupportedEncodingException, FileNotFoundException
    { 
        PrintWriter rval = openPrintWriter(theFile.getAbsolutePath(), append);
        return rval;
    }
    
    /**
     * Open a BufferedReader on fileName, "UTF-8"
     */
    public static BufferedReader openBufferedReader(String fileName)
        throws UnsupportedEncodingException, FileNotFoundException
    {
        BufferedReader rval = openBufferedReader(fileName, DEFAULT_ENCODING);
        return rval;
    }
    
    /**
     * Open a BufferedReader on File object, "UTF-8"
     */
    public static BufferedReader openBufferedReader(File aFile)
        throws UnsupportedEncodingException, FileNotFoundException
    {
        BufferedReader rval = openBufferedReader(aFile, DEFAULT_ENCODING);
        return rval;
    }
    
    /**
     * Open a BufferedReader on fileName, with specified encoding
     */
    public static BufferedReader openBufferedReader(String fileName, String encoding)
        throws UnsupportedEncodingException, FileNotFoundException
    {
        File theFile = new File(fileName);
        BufferedReader rval = openBufferedReader(theFile, encoding);
        return rval;
    }
    
    /**
     * Open a BufferedReader on File object, with specified encoding
     */
    public static BufferedReader openBufferedReader(File theFile, String encoding)
        throws UnsupportedEncodingException, FileNotFoundException
    {
        BufferedReader rval = new BufferedReader(new InputStreamReader(new FileInputStream(theFile), encoding));
        return rval;
    }
}