• 解决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);
  • 相关阅读:
    10-12
    8-10
    5.2-5.3
    四则运算 测试与封装
    第5-7章
    汉堡包
    1-5章
    实验二
    实验一
    Controller方法是如何与请求匹配上的及参数如何填充的
  • 原文地址:https://www.cnblogs.com/math-and-it/p/15401260.html
Copyright © 2020-2023  润新知