如何在输出文件中编写UTF-8格式

时间:2014-07-09 11:42:23

标签: java java-ee

我想用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.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();
          }
       }
     }

   }
}

我正在将XML转换为其他不使用特殊字符编写的格式。它显示了?????。

2 个答案:

答案 0 :(得分:0)

您可以使用此示例,

 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
   } `

答案 1 :(得分:0)

改为使用PrintWriter

File file =new File("C:\\Users\\abc\\Desktop\\Xml_format\\Done\\test.txt");

PrintWriter pw = new PrintWriter (file , 'UTF-8');
相关问题