xstream handles non-english character

Posted by Yan Cheng CHEOK on Stack Overflow See other posts from Stack Overflow or by Yan Cheng CHEOK
Published on 2010-06-15T18:18:43Z Indexed on 2010/06/15 18:22 UTC
Read the original article Hit count: 207

Filed under:

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.

1) May I know why System.out.println unable to print out Chinese Character in console?

2) I open up the xstream file. The saved value is

<string>???</string>

How can I make xstream save Chinese Character correctly?

Thanks.

© Stack Overflow or respective owner

Related posts about java