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