File file = new File(filename);
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
// directory does not exist create all directories in the filename
if (!parentFile.mkdirs()) {
throw new CompilerException(
"could not create dir structure needed to write file " + filename + " to disk"
);
}
}
FileOutputStream os = null;
try {
os = new FileOutputStream(filename);
os.write(writer.toByteArray());
} catch (IOException e) {
throw new CompilerException("could not write compiled class file to disk [" + filename + "]", e);
} finally {
try {
os.close();
} catch (IOException e) {
throw new CompilerException("could not close file output stream for [" + filename + "]", e);
}
}
}