[英]Java - transform xml file from ISO-8859-2 to UTF-8
我需要將文件從ISO-8859-2 charset轉換為UTF-8。
我的代碼是:
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
DOMSource domSource = new DOMSource(doc);
String fileName2 = UUID.randomUUID().toString() + "222";
Writer out = new OutputStreamWriter(new FileOutputStream("/Users/user/Kohana/" + fileName2 + ".xml"), "UTF8");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(domSource, new StreamResult(out));
但問題是,在轉換后,文件仍然是ISO-8859-2。
我究竟做錯了什么 ?
<?xml version=“1.0” encoding=“utf-8”?>
標題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.