properties文件编码乱码问题

 

properties默认的编码格式是ISO-8859-1。
我在idea的java项目读取properties时出现了中文乱码。
只需要修改idea的编码格式
File->Settings->File Encoding
properties文件编码乱码问题


再献上我的properties读取代码

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

public class MyPropertiesUtil {
    private static Logger logger = LoggerFactory.getLogger(MyPropertiesUtil.class);

    public static String getProperty(String pro, String key) {
        Properties properties = new Properties();
           try {
               properties.load(new InputStreamReader(MyPropertiesUtil.class.getClassLoader().getResourceAsStream(pro),"UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
               logger.error("配置文件读取异常",e);
        }
        String property = properties.getProperty(key);
        return property;
    }

    public static String getProperty(String pro, String key,String defaultValue){
        Properties properties = new Properties();
        try {
            properties.load(new InputStreamReader(MyPropertiesUtil.class.getClassLoader().getResourceAsStream(pro),"UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("配置文件读取异常",e);
        }

        String value = properties.getProperty(key.trim());
        if(StringUtils.isBlank(value)){
            value = defaultValue;
        }
        return value.trim();
    }

}

 

相关文章:

  • 2021-11-17
  • 2021-08-24
  • 2021-12-06
  • 2021-07-10
  • 2021-08-21
  • 2021-07-03
  • 2021-05-01
猜你喜欢
  • 2021-12-08
  • 2021-10-08
  • 2021-10-03
  • 2021-12-31
  • 2021-11-27
  • 2021-09-10
  • 2022-12-23
相关资源
相似解决方案