简体   繁体   中英

Spanish language encoding issue with Java Properties class load method

I am trying to encode the Spanish language for internationalization and use the Java Properties class load method to populate it to pass it to frontend.

I have tried to encode it using UTF-8 but still the accent characters are not coming correctly.

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Properties;

public class Message extends Properties {

    public static void main(String[] args) throws IOException {
        String spanish = "label=Sí";
        Message messages = new Message();
        messages.load(new ByteArrayInputStream(spanish.getBytes(Charset.forName("UTF-8"))));

        System.out.println(messages.get("label"));

    }

}

When i run the above code I am getting the text as "SÃ-". How can I retrieve the same text as "Sí"?

Try using ISO-8859-1 it gets expected result:

 messages.load(new ByteArrayInputStream(spanish.getBytes(Charset.forName("ISO-8859-1"))));

More about ISO-8859-1 can be obtained from this link: https://en.wikipedia.org/wiki/ISO/IEC_8859-1

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