简体   繁体   English

xstream处理非英语字符

[英]xstream handles non-english character

I have the following code : 我有以下代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helloworld;

import com.thoughtworks.xstream.XStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JOptionPane;

/**
 *
 * @author yccheok
 */
public class Test {
    @SuppressWarnings("unchecked")
    public static <A> A fromXML(Class c, File file) {
        XStream xStream = new XStream();
        InputStream inputStream = null;

        try {
            inputStream = new java.io.FileInputStream(file);
            Object object = xStream.fromXML(inputStream);
            if (c.isInstance(object)) {
                return (A)object;
            }
        }
        catch (Exception exp) {
            exp.printStackTrace();
        }
        finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                    inputStream = null;
                }
                catch (java.io.IOException exp) {
                    exp.printStackTrace();
                    return null;
                }
            }
        }

        return null;
    }

    @SuppressWarnings("unchecked")
    public static <A> A fromXML(Class c, String filePath) {
        return (A)fromXML(c, new File(filePath));
    }

    public static boolean toXML(Object object, File file) {
        XStream xStream = new XStream();
        OutputStream outputStream = null;

        try {
            outputStream = new FileOutputStream(file);
            xStream.toXML(object, outputStream);
        }
        catch (Exception exp) {
            exp.printStackTrace();
            return false;
        }
        finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                    outputStream = null;
                }
                catch (java.io.IOException exp) {
                    exp.printStackTrace();
                    return false;
                }
            }
        }

        return true;
    }

    public static boolean toXML(Object object, String filePath) {
        return toXML(object, new File(filePath));
    }

    public static void main(String args[]) {
        String s = "\u6210\u4EA4\u91CF";
        // print ???
        System.out.println(s);
        // fine! show 成交量
        JOptionPane.showMessageDialog(null, s);
        toXML(s, "C:\\A.XML");
        String o = fromXML(String.class, "C:\\A.XML");
        // show ???
        JOptionPane.showMessageDialog(null, o);
    }
}

I run the following code through command prompt in Windows Vista. 我在Windows Vista中通过命令提示符运行以下代码。

1) May I know why System.out.println unable to print out Chinese Character in console? 1)我可以知道为什么System.out.println无法在控制台中打印出汉字吗?

2) I open up the xstream file. 2)我打开xstream文件。 The saved value is 保存的值为

<string>???</string>

How can I make xstream save Chinese Character correctly? 如何使xstream正确保存汉字?

Thanks. 谢谢。

According to the XStream FAQ , it generates output (1) with whatever your platform default encoding is, and (2) without an XML prologue. 根据XStream FAQ ,它将生成(1)具有平台默认编码的输出,以及(2)没有XML序言的输出。 Which is a really bad combination. 这是一个非常糟糕的组合。

The FAQ recomments using toXml(Writer) . FAQ建议使用toXml(Writer) If you use an OutputStreamWriter , you can specify the encoding during construction. 如果使用OutputStreamWriter ,则可以在构造期间指定编码。 Since XStream doesn't emit a prologue, I recommend using "UTF-8", as that's what the XML spec requires. 由于XStream不会发出序幕,因此我建议使用“ UTF-8”,因为这是XML规范所要求的。

Alternatively, I suppose you could follow one of the other recommendations in the FAQ, and manually write an XML prologue into the stream with your default encoding. 另外,我想您可以遵循FAQ中的其他建议之一,并使用默认编码将XML序言手动写入流中。 I don't recommend that. 我不建议那样。

XStream xStream = new XStream(new DomDriver("UTF-8")); 

If the default character encoding on the platform isn't capable of displaying Chinese, you need to override it in the console and when launching Java. 如果平台上的默认字符编码无法显示中文,则需要在控制台中启动Java时覆盖它。 To set Java's character encoding, set the file.encoding property on the command-line (it won't work if you call System.setProperty() in your program). 要设置Java的字符编码,请在命令行上设置file.encoding属性(如果在程序中调用System.setProperty()则它将不起作用)。

java -Dfile.encoding=Big5 ...

I don't know the command for setting the console encoding in Vista. 我不知道在Vista中设置控制台编码的命令。 In Windows XP, it was the chcp ("change code page") command. 在Windows XP中,它是chcp (“更改代码页”)命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM