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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM