{
File parentDir = new File(targetFile.getParent());
IOUtil.createDir(parentDir, null);
CharArrayReader car = copy(reader);
XmlEncodingSniffer xes = new XmlEncodingSniffer(car, null);
Writer out = new OutputStreamWriter(new FileOutputStream(targetFile), xes.getXmlEncoding());
IOUtil.copyCompletely(car, out);
car.reset();
return car;
}