解决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);

 

posted @ 2021-10-13 11:14  数学与IT  阅读(773)  评论(0编辑  收藏  举报