代码之家  ›  专栏  ›  技术社区  ›  Cheok Yan Cheng

xstream处理非英语字符

  •  2
  • Cheok Yan Cheng  · 技术社区  · 15 年前

    /*
     * 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);
        }
    }
    

    我在WindowsVista中通过命令提示符运行以下代码。

    1) 我可以知道为什么System.out.println无法在控制台中打印汉字吗?

    <string>???</string>
    

    如何让xstream正确保存汉字?

    谢谢。

    3 回复  |  直到 15 年前
        1
  •  3
  •   facundofarias Ashay Batwal    9 年前

    根据 XStream FAQ

    FAQ推荐使用 toXml(Writer) . 如果你使用 OutputStreamWriter

    或者,我认为您可以按照FAQ中的其他建议之一,使用默认编码手动将XML序言写入流中。我不建议这样。

        2
  •  2
  •   Cheok Yan Cheng    15 年前
    XStream xStream = new XStream(new DomDriver("UTF-8")); 
    
        3
  •  1
  •   erickson    15 年前

    如果平台上的默认字符编码不能显示中文,则需要在控制台中覆盖它 启动Java时。要设置Java的字符编码,请设置 file.encoding 财产 在命令行上 (打电话就不行了 System.setProperty() 在您的程序中)。

    java -Dfile.encoding=Big5 ...
    

    我不知道在Vista中设置控制台编码的命令。在WindowsXP中,它是 chcp (“更改代码页”)命令。