Sometimes, We want to read the properties file that contains non-English characters.

For example, the properties file contains UTF-8 encoding characters. The default encoding for properties file reading is ISO-8859-1.

Spring framework loads the properties file in default encoding.

In this tutorial, you will learn how to read and write the content of a property file with a specified encoding in Java.

How to configure encoding for Spring boot to read

In this example, you will learn how to Read keys and their values from a property file spring boot application.

Let’s have a properties file - application_fr.properties which contains non-English characters.

database=mysql
hostname=localhost
username=john
password=

There are many ways property files are read in Spring core and boot applications.

One way is using PropertiesFactoryBean

  • create a Bean object returning PropertiesFactoryBean using the @Bean annotation

  • Inside create an object

  • set encoding using setFileEncoding method

  • configure to read properties file from ClassPathResource setLocation method

@Bean
public PropertiesFactoryBean propertiesfilemapping() {
    PropertiesFactoryBean factoryBean = new PropertiesFactoryBean();
    factoryBean.setFileEncoding("UTF-8");
    factoryBean.setLocation(new ClassPathResource("application_fr.properties"));
    return factoryBean;
}

Next, you can access the properties file mapped to member variables using @Valueannotation with the below syntax

@Value("#{propertiesfilemapping['key']}")
private String name;

Another way, using @PropertySource annotation with value containing the name of the file and encoding to `UTF-8

@PropertySource(value = “classpath:/application_properties.properties”, encoding=“UTF-8”)

Third way, Reading direct properties key to encoding

  • Configure PropertySource mapping
@PropertySource(value = "classpath:/application_properties.properties")

read individual properties with the getProperty method with encoding

propertiesObject.getProperty("name").getBytes("ISO-8859-1"), "UTF-8")

How to read from property file containing utf 8 character in java

Reading properties file is an easy way to do it in Java.

You can apply encoding for the file input stream as seen in the below example

import java.io.*;
import java.nio.charset.Charset;
import java.util.Properties;
import java.util.Set;

public class PropertiesReader {
    public static void main(String[] args) {
        Properties properties = new Properties();
        File file = new File("B:\\application.properties");

        try {
            FileInputStream inputStream = new FileInputStream(file);

            properties.load(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
        } catch (FileNotFoundException fie) {
            fie.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(properties.getProperty("hostname"));
        Set<String> keys = properties.stringPropertyNames();
        for (String key : keys) {
            System.out.println(key + " - " + properties.getProperty(key));
        }
    }
}

Conclusion

You learned how to configure properties files to read UTF-8 character encoding in Spring framework and boot as well as Java.