Java 如何在输出文件中写入UTF-8格式
我想用UTF-8编写输出文件,但找不到方法。有人能帮我用UTF-8写xml吗Java 如何在输出文件中写入UTF-8格式,java,Java,我想用UTF-8编写输出文件,但找不到方法。有人能帮我用UTF-8写xml吗 import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ReadXMLFile2 {
private static byte[] contentInBytes;
public static void main(String[] args) {
try {
File file = new File("C:\\Users\\abc\\Desktop\\Xml_format\\Done\\part3.xml");
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = dBuilder.parse(file);
// System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
if (doc.hasChildNodes()) {
printNote(doc.getChildNodes());
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static void printNote(NodeList nodeList) {
for (int count = 0; count < nodeList.getLength(); count++) {
Node tempNode = nodeList.item(count);
// make sure it's element node.
if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
System.out.println("FIELD" +""+ tempNode.getNodeName() + "=" + tempNode.getTextContent());
if (tempNode.hasAttributes()) {
// get attributes names and values
NamedNodeMap nodeMap = tempNode.getAttributes();
for (int i = 0; i < nodeMap.getLength(); i++) {
Node node = nodeMap.item(i);
System.out.println("attr name : " + node.getNodeName());
System.out.println("attr value : " + node.getNodeValue());
}
}
if (tempNode.hasChildNodes()) {
// loop again if has child nodes
printNote(tempNode.getChildNodes());
}
System.out.println(tempNode.getNodeName());
try {
File file =new File("C:\\Users\\abc\\Desktop\\Xml_format\\Done\\test.txt");
String data ="#DREFIELD" +""+ tempNode.getNodeName() + "=" + tempNode.getTextContent()+"";
//if file doesnt exists, then create it
if(!file.exists()){
file.createNewFile();
}
//true = append file
FileWriter fw = new FileWriter(file,true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(data);
bw.close();
System.out.println("Done");
} catch(IOException e){
e.printStackTrace();
}
}
}
}
}
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileWriter;
导入java.io.IOException;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入org.w3c.dom.Document;
导入org.w3c.dom.NamedNodeMap;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
公共类ReadXMLFile2{
私有静态字节[]contentInBytes;
公共静态void main(字符串[]args){
试一试{
File File=新文件(“C:\\Users\\abc\\Desktop\\Xml\u format\\Done\\part3.Xml”);
DocumentBuilder dBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc=dBuilder.parse(文件);
//System.out.println(“根元素:+doc.getDocumentElement().getNodeName());
if(doc.hasChildNodes()){
printNote(doc.getChildNodes());
}
}捕获(例外e){
System.out.println(e.getMessage());
}
}
私有静态void打印注释(节点列表节点列表){
对于(int count=0;count
我正在将XML转换为其他格式,这些格式不是用特殊字符编写的。它显示了什么。您可以使用此示例:
try {
File fileDir = new File("c:\\temp\\test.txt");
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fileDir), "UTF8"));
out.append("data");
out.flush();
out.close();
}
catch (UnsupportedEncodingException e)
{
//todo
}
catch (IOException e)
{
//todo
}
catch (Exception e)
{
//todo
}
改用
说真的,你在提交这个问题之前没有发现这个问题吗?谢谢,它起作用了,但我正在尝试读取包含多个节点的xml文件,但它没有写入整个输出。它不是迭代整个XML。您想做什么?您从多个节点读取并写入单个文件?我希望读取一个xml文件,该文件具有多个节点并写入单个文件,但不在单个文件中打印整个输出。是的。您可以将上述代码与线程一起使用。另外,请与vfsmanager一起检查apache commons vfs。它可用于处理多个文件系统。
File file =new File("C:\\Users\\abc\\Desktop\\Xml_format\\Done\\test.txt");
PrintWriter pw = new PrintWriter (file , 'UTF-8');