spring boot读取resource下的文件,支持jar方式启动,防止中文乱码

1.工程结构

spring boot读取resource下的文件,支持jar方式启动,防止中文乱码_第1张图片

2.代码

Resource resource = new ClassPathResource("system/configuration.json");InputStream inputStream=null;
BufferedReader bufferedReader=null;
try {
   
    inputStream=resource.getInputStream();
    bufferedReader=  new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
} catch (IOException e) {
    e.printStackTrace();
}
String b ;
StringBuffer stringBuffer=new StringBuffer();
try {
    while ((b=bufferedReader.readLine())!=null){
        stringBuffer.append(b);
    }
} catch (IOException e) {
    e.printStackTrace();
}finally {
        try {
            if(inputStream!=null){
                inputStream.close();
            }
            if(bufferedReader!=null){
                bufferedReader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

}
     System.out.println(stringBuffer.toString());

你可能感兴趣的:(spring,boot,java)