==========================================================================================================================================================
1,间接访问jar包中的文件(其他jar包)
当前类:com.java.filepath.TestJarFile
资源文件:URL: jar:file:/F:/worklog/20150408/test/lib/test.jar!/config/db.cfg.xml ,不在当前类的jar包
public class TestJarFile {
public static void main(String[] args) throws IOException {
JarFile jf = new JarFile("F:/worklog/20150408/test/lib/test.jar");
JarEntry entry = jf.getJarEntry("config/db.cfg.xml");
InputStream input = jf.getInputStream(entry);
InputStreamReader isr = new InputStreamReader(input);
BufferedReader reader = new BufferedReader(isr);
String s = null;
while((s=reader.readLine())!=null){
System.out.println(s);
}
reader.close();
}
}
2,在jar包中直接访问当前jar包中的文件(当前类所在jar包)
当前类:com.java.filepath.TestURL
资源文件:com.config.db.cfg.xml
共同在一个jar包里
public class TestURL {
public static void main(String[] args) throws IOException {
//读取jar包中的文件内容
BufferedReader br = new BufferedReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream("com/config/db.cfg.xml")));
String buf = null;
while((buf=br.readLine() )!= null){
System.out.println(buf);
}
br.close();
}
}
==========================================================================================================================================================
设置使得配置文件可以在jar包中或者jar包外:
1,设置log4j的文件路径,可以被配置在jar包里(变更起来比较麻烦)
比如:
log4j的配置文件路径:"config.log4j.properties"
可以在入口程序类如下设置:
static{
PropertyConfigurator.configure(Start.class.getClassLoader().getResource("config/log4j.properties"));
}
2,设置hibernate的配置文件路径,可以被配置在jar包里
如:
hibernate配置文件所在完整路径是 "config.db.cfg.xml"
HibernateSessionFactory.java 所在路径是 "com.oki.util.HibernateSessionFactory.java"
设置 new org.hibernate.cfg.Configuration().configure(Start.class.getClassLoader().getResource("config/db.cfg.xml")); ,这样在程序打成jar包或Eclipse调试时都能访问。
配置jar包中的框架配置文件,访问jar包中的文件
最新推荐文章于 2024-06-12 10:51:59 发布