-
问题内容:Jar包中配置文件的读取/修改
- 原讨论链接:http://community.csdn.net/expert/topicview1.asp?id=5498624
- 所属论坛:J2SE / 基础类
审核组:JAVA
- 提问者:fgt___73
解决者:CrazyGou
- 感谢:
- 关键字:Java J2SE / 基础类 修改 读取 jar 配置文件
- 答案:
配置文件和类文件一起打包到一个Jar中,如何读取/修改这个配置文件?
---------------------------------------------------------------
读取:
String currentJarPath = URLDecoder.decode(YourClassName.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8"); //获取当前Jar文件名,并对其解码,防止出现中文乱码
JarFile currentJar = new JarFile(currentJarPath);
JarEntry dbEntry = currentJar.getJarEntry("包名/配置文件");
InputStream in = currentJar.getInputStream(dbEntry);
//以上YourClassName是class全名,也就是包括包名
修改:
JarOutputStream out = new FileOutputStream(currentJarPath);
out.putNextEntry(dbEntry);
out.write(byte[] b, int off, int len); //写配置文件
。。。
out.close();
- 评价:
给朵鲜花(4)
扔个鸡蛋(0)