나는 다음과 같은 코드가 있습니다XStream을 영어가 아닌 문자를 처리
/*
* 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);
}
}
내가 Windows Vista에서 명령 프롬프트를 통해 다음 코드를 실행합니다.
1) System.out.println이 콘솔에서 중국어 문자를 출력 할 수없는 이유를 알고 싶습니다.
2) xstream 파일을 엽니 다. 저장된 값은
<string>???</string>
어떻게 xstream 저장 한자를 올바르게 만들 수 있습니까?
감사합니다.
왜 "UTF-16"인코딩을 사용하지 않아야합니까? 자바 문자열 저장소 데이터가 UTF-16이 아닌가요? –
UTF-16을 사용할 수 있습니다. 중국어 텍스트의 경우 UTF-16 파일은 UTF-8 파일보다 작아야합니다. 그러나 만약 당신이 그 경로를 가면, 파일의 시작 부분에 BOM을 내 보내야하는데, 나는'OutputStreamWriter'가 자동적으로 그렇게한다고 믿지 않습니다. 인코딩에 대한 자세한 내용은 http://www.xml.com/axml/testaxml.htm을 참조하십시오. – Anon