Java读取jar包中的文件
程序员文章站
2022-03-02 21:09:01
...
一、使用Resource
public void getFileInfo() throws Exception {
Map<String, String> fileMap = new HashMap<>();
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resourcePatternResolver.getResources("classpath:*");
for (Resource resource : resources) {
String fileName = resource.getFilename();
String filePath = resource.getURL().toString();
byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream());
// 文件内容
String fileContent = Base64Utils.encodeToString(bytes);
fileMap.put(fileName,filePath);
}
}
二、使用ClassPathResource
public void getFileInfo() throws Exception {
ClassPathResource classPathResource = new ClassPathResource("类加载路径下的子路径");
InputStream inputStream = classPathResource.getInputStream();
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
String stringContent = new String(bytes);
}
三、使用类加载器
public void getFileInfo(String path) throws Exception {
BufferedReader in = new BufferedReader(
new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(path)));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
}
String input = buffer.toString();
}
上一篇: scrapy之Items
下一篇: Scrapy-5.Items