code: public static OutputStream getFileOutputStream(String filename) throws IOException {
      OutputStream out = new FileOutputStream(filename);
      if (filename.endsWith(".gz")) {
        out = new GZIPOutputStream(out);
      } else if (filename.endsWith(".bz2")) {
        //out = new CBZip2OutputStream(out);
        out = getBZip2PipedOutputStream(filename);
      }
      return out;
    }
docs: Get a output file stream (automatically gzip/bzip2 depending on file extension)
  @param filename Name of file to open
  @return Output stream that can be used to write to the file
  @throws IOException if there are exceptions opening the file