I am reading a property file which consists of a message in the UTF-8 character set.
Problem
The output is not in the appropriate format.
Use props.load(new FileReader("uinsoaptest.properties"))
instead. By default it uses the encoding Charset.forName(System.getProperty("file.encoding"))
which can be set to UTF-8 with System.setProperty("file.encoding", "UTF-8")
or with the commandline parameter -Dfile.encoding=UTF-8
.
Use an InputStreamReader
with Properties.load(Reader reader)
:
FileInputStream input = new FileInputStream(new File("uinsoaptest.properties"));
props.load(new InputStreamReader(input, Charset.forName("UTF-8")));
You should specify the UTF-8 encoding when you construct your FileInputStream object. You can use this constructor:
new FileInputStream("uinsoaptest.properties", "UTF-8");
If you want to make a change to your JVM so as to be able to read UTF-8 files by default, you will have to change the JAVA_TOOL_OPTIONS in your JVM options to something like this :
-Dfile.encoding=UTF-8
If somebody use @Value annotation, could try StringUils.
@Value("${title}")
private String pageTitle;
public String getPageTitle() {
return StringUtils.toEncodedString(pageTitle.getBytes(Charset.forName("ISO-8859-1")), Charset.forName("UTF-8"));
}