kk.jar的目录结构如下
+kk.jar
+META-INF
MANIFEST.MF
+config
database.properties
目标:读取database.properties中的参数信息
types=mysql
mysql.url=jdbc:mysql://127.0.0.1:3306/tjtz
mysql.user=root
mysql.password=root
mysql.driver=com.mysql.jdbc.Driver
mysql.maxCounts=5
代码如下:
/*
* @(#)JarFileReader.java Oct 9, 2008
*
* Copyright (c) 2008 by jadmin. All Rights Reserved.
*/
package file.jar;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* 从jar包中读取配置文件信息
*
* @author <a href="mailto:jadmin@yeah.net">jadmin</a>
* @version 1.00 Oct 9, 2008 1:10:44 AM
*/
public class JarReader {
public static void main(String[] args) {
String jarPath = "C:\\Documents and Settings\\Administrator\\桌面\\新建文件夹\\kk.jar";
String entryFile = "config/database.properties";
doRead(jarPath,entryFile);
}
public static void doRead(String jarPath, String entryFile) {
try {
JarFile jarFile = new JarFile(jarPath);
JarEntry entry = jarFile.getJarEntry(entryFile);
InputStream input = jarFile.getInputStream(entry);
process(input);
jarFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void process(InputStream input) {
Properties p = new Properties();
try {
p.load(input);
Set<?> set = p.keySet();
for (Object name : set)
System.out.println(name + "=" + p.getProperty((String) name));
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果如下:
posted on 2008-10-09 01:49
jadmin 阅读(1735)
评论(0) 编辑 收藏