美文网首页
SpringBoot读取resources目录下文件

SpringBoot读取resources目录下文件

作者: AC编程 | 来源:发表于2021-12-31 10:22 被阅读0次
     /**
         * SpringBoot 读取resources目录下文件
         * 如读取文件:src/main/resources/alipay/appCertPublicKey.crt
         * @return
         */
        private String readResourcesFile(){
            String path="alipay/appCertPublicKey.crt";
            org.springframework.core.io.Resource resource = new ClassPathResource(path);
            StringBuffer contentBuffer = new StringBuffer();
            InputStream inputStream = null;
            InputStreamReader streamReader = null;
            BufferedReader bufferedReader = null;
            String content;
            try{
                inputStream = resource.getInputStream();
                streamReader = new InputStreamReader(inputStream);
                bufferedReader = new BufferedReader(streamReader);
    
                while((content = bufferedReader.readLine()) != null) {
                    contentBuffer.append(content+"\n");
                }
                log.info(path+":content:"+contentBuffer.toString());
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try{
                    if(bufferedReader!=null){
                        bufferedReader.close();
                    }
    
                    if(streamReader!=null){
                        streamReader.close();
                    }
    
                    if(inputStream!=null){
                        inputStream.close();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
    
            return contentBuffer.toString();
        }
    

    相关文章

      网友评论

          本文标题:SpringBoot读取resources目录下文件

          本文链接:https://www.haomeiwen.com/subject/tvdgqrtx.html