简体   繁体   中英

Invalid byte 2 of 2-byte UTF-8 Java, sequence error depending on Windows/IntelliJ

So the same code is running on both of the devices, both PCs are running Windows 10, but the variable is represented different in IntelliJ debugger and one can decode string, and other can't. Both are accessing same endpoint. This is code snippet that's throwing error and where code breaks. Both code are running on the same JRE and getting variable value from same endpoint.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file);

As I am generating XML file that's UTF-8 encoded, it throws error for that variable.

Variable that's not throwing error on PC1 shown in debugger

调试器 var 不抛出错误

Variable that's throwing error on PC2

误差变量

I tried reinstalling all JRE, IntelliJ, etc etc, still no help...

Are you sure the file is UTF-8 encoded? In my case I realized it was windows-1250 encoding used. So I had to specify an encoding:

InputStream is = ...
InputSource src = new InputSource(new InputStreamReader(is, Charset.forName("cp1250")));
Document xmlDocument = builder.parse(src);

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