java 读取jar包中的文件

淩亂°似流年 2022-05-14 05:13 320阅读 0赞

参考: https://blog.csdn.net/rchm8519/article/details/39557499

参考: https://www.cnblogs.com/zeciiii/p/4178824.html

需求:

我自己写了一个Java 的记事本工具,然后实现快捷键功能,于是就把快捷键对应的key-value 保存到properties文件中,

类似:

Ctrl,S=\u4FDD\u5B58
Ctrl,P=\u53E6\u5B58\u4E3A
Ctrl,N=\u65B0\u5EFA
Ctrl,Esc=\u9000\u51FA

问题:

但是发现,当将项目打成jar 包以后,在jar 包内部读取jar包内部的文件,一直都行不通,因为路径问题,从外部读取jar包中的内容,可以通过: …jar!/文件路径,找到内容,但是自己本身读取自己,就会总是FileNotFoundException ,后来发现有个专门读取jar包内容的JarFile

从外部读取jar包内容实例:

  1. public static Map<String, String> init(String defaultPath) {
  2. if(CheckUtil.empty(defaultPath)){
  3. defaultPath = ReadProperties.defaultPath;
  4. }
  5. String packagePath = "/file/"+defaultPath;
  6. URL resource = ReadProperties.class.getResource(packagePath);//根据 source 文件下的包路径,得到url,必须是绝对路径
  7. String path = resource.getFile();//根据url 得到完整的 路径
  8. PropertiesParse parse = new PropertiesParse();
  9. InputStream input = null;
  10. if(path.contains(".jar")) {
  11. //得到jar目录
  12. String jarPath = path.substring(0, path.indexOf("!"));
  13. //得到jarFile 对象
  14. JarFile jarFile = null;
  15. try {
  16. jarFile = new JarFile(jarPath);
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }
  20. ZipEntry entry = jarFile.getEntry(packagePath.substring(1));//这里是相对路径,前面没有斜杠/
  21. try {
  22. input = jarFile.getInputStream(entry);
  23. } catch (IOException e) {
  24. // TODO Auto-generated catch block
  25. e.printStackTrace();
  26. }
  27. }else{//正常class 读取,普通File 读取
  28. try {
  29. input = new FileInputStream(new File(path));
  30. } catch (FileNotFoundException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. }
  35. parse.setSrcToReader(input);
  36. Map<String, String> map = parse.getMap();
  37. return map;
  38. }
  39. public static void main(String[] args) {
  40. String defaultPath = "";
  41. String packagePath = "/file/"+"keyEvent.properties";
  42. // 读取jar包中的文件,参考地址; https://blog.csdn.net/rchm8519/article/details/39557499,https://www.cnblogs.com/zeciiii/p/4178824.html
  43. String str = "/C:/Users/12198/Desktop/KEditPad.jar";// /file/keyEvent.properties
  44. JarFile jarFile = null;
  45. PropertiesParse parse = new PropertiesParse();
  46. InputStream input = null;
  47. try {
  48. jarFile = new JarFile(str);//通过jarFile 读取jar包
  49. } catch (IOException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }
  53. //遍历entry 对象
  54. Enumeration<JarEntry> entries = jarFile.entries();
  55. while(entries.hasMoreElements()){
  56. System.out.println(entries.nextElement().getName());
  57. }
  58. ZipEntry entry = jarFile.getEntry(packagePath.substring(1));//name 最开头没有/,换句话就是相对路径
  59. try {
  60. input = jarFile.getInputStream(entry);
  61. } catch (IOException e) {
  62. // TODO Auto-generated catch block
  63. e.printStackTrace();
  64. }
  65. parse.setSrcToReader(input);
  66. Map<String, String> map = parse.getMap();
  67. }

得到了文件路径,通过File 的new File(str),会报FileNotFound 异常,java 读取jar包中的文件,应该用JarFile 类来读取

jar包内部读取jar包内容实例:(外部或者项目中也可通过此方式)

如果file放在源码目录(例如:我把文件放在src下的file包下)下,则无论是在外部读取还是在jar包中读取,都可通过classLoader

  1. public static Map<String, String> init(String defaultPath) {
  2. InputStream stream =
  3. ClassLoader.getSystemResourceAsStream("file/keyEvent.properties");
  4. PropertiesParse parse = new PropertiesParse();
  5. parse.setSrcToReader(stream);
  6. Map<String, String> map = parse.getMap();
  7. return map;
  8. }

发表评论

表情:
评论列表 (有 0 条评论,320人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java读取jar文件

    java代码读取文件问题解决 问题背景 通过java代码读取文件的方式有很多种,那么我们应该能理解,不同的场景应该使用不同的读取方式,我在前段时间开发项目的时候遇到