简体   繁体   中英

SWT browser does not accept the argument string encoded cp1251

I have a xml file encoded cp1251 and I want to view it in swt browser, but browser.getText() return null if I run this code.

//....
String fileEncoding = "UTF-8"
byte[] encoded = Files.readAllBytes(Paths.get(file.toURI()));
String text = new String(encoded,fileEncoding);
browser.setText(text); 

I had read that browser set string encoded Unicode, mb I wrong encoded string from cp1251 to utf-8, or has another way to see my file in browser

Maybe you should change the UTF-8 encoding to cp1251 ?

Because java internally use own encoding UTF-16, you need to tell in which encoding you read your file, and you read it as UTF-8, but i think you need to read it as cp1251, bacause you said that your xml is encoded in cp1251, so you should read it like this:

String fileEncoding = "cp1251"
byte[] encoded = Files.readAllBytes(Paths.get(file.toURI()));
String text = new String(encoded,fileEncoding);
browser.setText(text); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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