jar包在linux下读取resources下文件

2022年8月20日09:16:13

废话少说 放码

/**
     * 读取Resources下资源文件
     * jar包时 linux下也可用
     * @param filePath 文件路径 例如 /topology/color.json 注意/
     * @return String 文件内容
     */privatestaticStringreadFileStreamToStr(String filePath){InputStream is=null;BufferedReader br=null;try{
            is=FileReadUtils.class.getResourceAsStream(filePath);if(null== is){
                log.error("readFile: file is not found, file path: {}", filePath);returnnull;}

            br=newBufferedReader(newInputStreamReader(is));String s;StringBuilder sb=newStringBuilder();while((s= br.readLine())!=null){
                sb.append(s).append("\r\n");}return sb.toString();}catch(IOException e){
            log.error(e.getCause().toString());}finally{if(is!=null){try{
                    is.close();}catch(IOException e){
                    log.error("error : FileReadUtils-readLinuxFileToStr-close InputStream : {}", e.getMessage());}}if(br!=null){try{
                    is.close();}catch(IOException e){
                    log.error("error : FileReadUtils-readLinuxFileToStr-close BufferedReader : {}", e.getMessage());}}}returnnull;}
  • 作者:徐静静真帅啊
  • 原文链接:https://blog.csdn.net/qq_42595758/article/details/123919420
    更新时间:2022年8月20日09:16:13 ,共 868 字。