zoukankan      html  css  js  c++  java
  • 解决springboot打成jar包后, 无法获取(classpath)类路径下的自定义配置文件

    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;
    import java.nio.charset.Charset;
    import java.io.*; 
    
    public static String getInJarTextFile(String path, Charset charset) throws IOException {
        Resource resource = new ClassPathResource(path);
        StringBuilder sb = new StringBuilder();
        try(BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(), charset))) {
            String temp;
            while ((temp = br.readLine()) != null) {
                sb.append(temp);
            }
            return sb.toString();
        }
    }

    使用参考

    import java.nio.charset.StandardCharsets;

    String json = FileUtil.getInJarTextFile("config.json", StandardCharsets.UTF_8);
  • 相关阅读:
    动画,缩放
    本地公司
    调研 公司信息
    外贸 内贸 经商
    情感 爱情 恋爱
    H5源码
    世界 生活
    标识
    Jackson
    java数据结构
  • 原文地址:https://www.cnblogs.com/math-and-it/p/15401260.html
Copyright © 2011-2022 走看看