import com.google.common.base.Charsets; import com.google.common.io.CharStreams; private static void readJsonFile(){ InputStream inputStream = MyMainApp.class.getClassLoader().getResourceAsStream("test.json"); String testStr= CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8)); }
pom.xml中
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> <include>**/*.json</include> </includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources>
打包后的jar中的真实目录是BOOT-INF/classes/test.json