public Writer getOutputFileWriter(Grammar g, String fileName) throws IOException {
		if (outputDirectory == null) {
			return new StringWriter();
		}
		// output directory is a function of where the grammar file lives
		// for subdir/T.g4, you get subdir here.  Well, depends on -o etc...
		File outputDir = getOutputDirectory(g.fileName);
		File outputFile = new File(outputDir, fileName);

		if (!outputDir.exists()) {
			outputDir.mkdirs();
		}
		FileOutputStream fos = new FileOutputStream(outputFile);
		OutputStreamWriter osw;
		if ( grammarEncoding!=null ) {
			osw = new OutputStreamWriter(fos, grammarEncoding);
		}
		else {
			osw = new OutputStreamWriter(fos);
		}
		return new BufferedWriter(osw);
	}
This method is used by all code generators to create new output
 files. If the outputDir set by -o is not present it will be created.
 The final filename is sensitive to the output directory and
 the directory where the grammar file was found.  If -o is /tmp
 and the original grammar file was foo/t.g4 then output files
 go in /tmp/foo.

 The output dir -o spec takes precedence if it's absolute.
 E.g., if the grammar file dir is absolute the output dir is given
 precedence. "-o /tmp /usr/lib/t.g4" results in "/tmp/T.java" as
 output (assuming t.g4 holds T.java).

 If no -o is specified, then just write to the directory where the
 grammar file was found.

 If outputDirectory==null then write a String.
tool/src/org/antlr/v4/Tool.java