Here you can find the source of saveTemporaryDocument(Document document, String folderName)
private static void saveTemporaryDocument(Document document, String folderName) throws IOException, Exception
//package com.java2s; //License from project: Mozilla Public License import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import org.w3c.dom.Document; import org.w3c.dom.bootstrap.DOMImplementationRegistry; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSSerializer; public class Main { private static int index = 0; private static void saveTemporaryDocument(Document document, String folderName) throws IOException, Exception { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); String str = writer.writeToString(document); str = str.replace("UTF-16", "UTF-8"); File outputFolder = new File(folderName); outputFolder.mkdirs();/*from ww w .j av a 2 s .c om*/ String fileName = outputFolder.getAbsolutePath() + "/" + "file_" + String.valueOf(index) + ".xml"; OutputStreamWriter outWriter = null; try { outWriter = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"); outWriter.write(str); } finally { if (outWriter != null) { outWriter.close(); } } index++; } /** * TODO Description. * * @param fileName * @param destination */ public static void write(File fileName, String destination) throws IOException { File outputFolder = new File(destination); outputFolder.mkdirs(); Writer outWriter = null; try { BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName)); outWriter = new OutputStreamWriter(new FileOutputStream(outputFolder), "UTF-8"); String line = null; while ((line = bufferedReader.readLine()) != null) { outWriter.write(line); } } finally { if (outWriter != null) { outWriter.close(); } } } }