Reading from a properties file containing utf 8 characters

I am reading a properties file that consists of a message in a UTF-8 character set.

Problem

The output is not in the appropriate format. I am using InputStream .

The properties file looks like

 username=LBSUSER password=Lbs@123 url=http://localhost:1010/soapfe/services/MessagingWS timeout=20000 message=Spanish character are = {á é í, ó,ú ,ü, ñ, ç, å, Á, É, Í, Ó, Ú, Ü, Ñ, Ç, ¿, °, 4° año = cuarto año, €, ¢, £, ¥} 

And I read the file like this,

 Properties props = new Properties(); props.load(new FileInputStream("uinsoaptest.properties")); String username = props.getProperty("username", "test"); String password = props.getProperty("password", "12345"); String url = props.getProperty("url", "12345"); int timeout = Integer.parseInt(props.getProperty("timeout", "8000")); String messagetext = props.getProperty("message"); System.out.println("This is soap msg : " + messagetext); 

Result of the above message:

enter image description here

You can see the message in the console after the line

{************************ SOAP MESSAGE TEST***********************}

I would appreciate it if I could help by reading this file correctly. I can read this file with a different approach, but I am looking for less modification code.

+13
java encoding parsing utf-8
source share
4 answers

Use InputStreamReader with Properties.load(Reader reader) :

 FileInputStream input = new FileInputStream(new File("uinsoaptest.properties")); props.load(new InputStreamReader(input, Charset.forName("UTF-8"))); 
+44
source share

Use props.load(new FileReader("uinsoaptest.properties")) instead. The default encoding is Charset.forName(System.getProperty("file.encoding")) , which can be set to UTF-8 using System.setProperty("file.encoding", "UTF-8") or with command line parameter -Dfile.encoding=UTF-8 .

+2
source share

If someone uses the @Value annotation, you can try StringUils.

 @Value("${title}") private String pageTitle; public String getPageTitle() { return StringUtils.toEncodedString(pageTitle.getBytes(Charset.forName("ISO-8859-1")), Charset.forName("UTF-8")); } 
+1
source share

You must specify the UTF-8 encoding when creating the FileInputStream object. You can use this constructor:

 new FileInputStream("uinsoaptest.properties", "UTF-8"); 

If you want to make changes to your JVM in order to be able to read UTF-8 files by default, you will need to change the JAVA_TOOL_OPTIONS in your JVM settings to something like this:

 -Dfile.encoding=UTF-8 
0
source share

All Articles