解決讀取properties配置文件中文亂碼問題

        在使用Environment讀取參數時,如果裏面有中文讀到的一般會是亂碼,網上一般都是推薦的修改idea的編碼等,但這樣做你可能會不生效。

         你可以通過以下方法來讀取配置文件,手動設置爲指定編碼來讀取。

 public static String readPropertiesFile(String key) throws FileNotFoundException, IOException {
        try {
            Resource fileRource =  new ClassPathResource("application.properties");
            Properties props = new Properties();
            props.load(new InputStreamReader(fileRource.getInputStream(), "UTF-8"));
            String value = props.getProperty(key);
            System.out.println(key + "鍵的值是:" + value);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章