InputStream转换中文乱码

在new InputStreamReader()要指定charset避免中文乱码

public class PropertiesDemo {
    private static Logger logger = LoggerFactory.getLogger(PropertiesDemo.class);
    private static Properties props = new Properties();

    static {
        readConfig();
    }

    private static void readConfig(){
        InputStream inputStream = PropertiesDemo.class.getClassLoader().getResourceAsStream("database.properties");
        try {
            Reader reader = new InputStreamReader(inputStream, "UTF-8");
            props.load(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String getProperty(String key){
        String property = props.getProperty(key.trim());
        return property.trim();
    }

    public static void main(String[] args) {
        String property = getProperty("ip");
        System.out.println(property);
    }

}

你可能感兴趣的:(javaee)