怎么修改jar包里的xml文件

前端之家收集整理的这篇文章主要介绍了怎么修改jar包里的xml文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1.  
  2. static Properties readJarProperty() throws IOException {
  3. currentJarPath = URLDecoder.decode(com.taisys.ota.util.RunMain.class
  4. .getProtectionDomain().getCodeSource().getLocation().getFile(),"UTF-8"); // 获取当前Jar文件名,并对其解码,防止出现中文乱码
  5. System.out.println("currentJarPath=" + currentJarPath);
  6. JarFile currentJar = new JarFile(currentJarPath);
  7. JarEntry dbEntry = currentJar
  8. .getJarEntry("com/taisys/ota/util/jdbc.properties");
  9. InputStream in = currentJar.getInputStream(dbEntry);
  10. Properties p = new Properties();
  11. p.load(in);
  12. in.close();
  13. return p;
  14. }
  15. static void updateJarProperty() throws IOException {
  16. currentJarPath = URLDecoder.decode(com.taisys.ota.util.RunMain.class
  17. .getProtectionDomain().getCodeSource().getLocation().getFile(),"UTF-8"); // 获取当前Jar文件名,并对其解码,防止出现中文乱码
  18. String EntryName = "jdbc.properties";
  19. currentPath = startServer();
  20. byte[] data = null;
  21. File properFile = new File(currentPath + "/jdbc.properties");
  22. if (properFile.exists()) {
  23. InputStream in = new FileInputStream(properFile);
  24. data = getByte(in);
  25. }
  26. try {
  27. JarFile jf = new JarFile(currentJarPath);
  28. TreeMap<String,byte[]> tm = new TreeMap<String,byte[]>();
  29. Enumeration es = jf.entries();
  30. while (es.hasMoreElements()) {
  31. JarEntry je = (JarEntry) es.nextElement();
  32. byte[] b = getByte(jf.getInputStream(je));
  33. tm.put(je.getName(),b);
  34. }
  35. JarOutputStream out = new JarOutputStream(new FileOutputStream(
  36. currentJarPath));
  37. Set set = tm.entrySet();
  38. Iterator it = set.iterator();
  39. boolean has = false;
  40. while (it.hasNext()) {
  41. Map.Entry me = (Map.Entry) it.next();
  42. String name = (String) me.getKey();
  43. JarEntry jeNew = new JarEntry(name);
  44. // System.out.println("jeNew.getName()="+jeNew.getName());
  45. out.putNextEntry(jeNew);
  46. byte[] b;
  47. if (name.equals(EntryName)) {
  48. b = data;
  49. has = true;
  50. } else
  51. b = (byte[]) me.getValue();
  52. out.write(b,b.length);
  53. }
  54. if (!has) {
  55. JarEntry jeNew = new JarEntry(EntryName);
  56. out.putNextEntry(jeNew);
  57. out.write(data,data.length);
  58. }
  59. out.flush();
  60. out.finish();
  61. out.close();
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. }
  65. }

猜你在找的XML相关文章