public static BufferedWriter prepFileForWriting(String outputFilename) throws Exception {
        BufferedWriter writer;
        File outputFile = new File(outputFilename);
        if (outputFile.exists() && !outputFile.delete()) {
            throw new Exception("Could not delete existing file: " + outputFilename);
        }
        try {
            if (!outputFile.createNewFile()) {
                throw new Exception("Did not delete existing file: " + outputFilename);
            }
            if (!outputFile.canWrite()) {
                throw new Exception("Could not write to file: " + outputFilename);
            }
        } catch (IOException e) {
            throw new Exception("Could not create file for writing: " + outputFilename + "\n" + e.getMessage());
        }
        try {
            writer = new BufferedWriter(new FileWriter(outputFile));
        } catch (IOException e) {
            throw new Exception("Could not create stream for writing: " + outputFilename + "\n" + e.getMessage());
        }
        return writer;
    }