我有一个编码为cp1251的xml文件,我想在swt浏览器中查看它,但如果我运行此代码,browser.getText()将返回null。
//....
String fileEncoding = "UTF-8"
byte[] encoded = Files.readAllBytes(Paths.get(file.toURI()));
String text = new String(encoded,fileEncoding);
browser.setText(text); 我读过浏览器设置字符串编码为Unicode,m.b.我错误地将编码字符串从cp1251转换为utf-8,或者有其他方法在浏览器中查看我的文件
发布于 2015-05-22 20:24:59
也许你应该把UTF-8编码改成cp1251?
因为java在内部使用了自己的编码UTF-16,所以你需要告诉你是用哪种编码读取你的文件的,你把它读成UTF-8,但是我认为你需要把它读成cp1251,因为你说你的xml是用cp1251编码的,所以你应该这样读它:
String fileEncoding = "cp1251"
byte[] encoded = Files.readAllBytes(Paths.get(file.toURI()));
String text = new String(encoded,fileEncoding);
browser.setText(text); https://stackoverflow.com/questions/30396639
复制相似问题