public static void main(String[] args) throws IOException {
String jarPath = "omc-pm-server-eOMC910V100R001C00SPC100B020.jar";
String res = readFileFromJar(jarPath,"pm.hbm.xml");
System.out.println(res);
}
/**
* 从jar中获取指定名称的文本文件
*
* @param jarPath
* @param name
* @throws IOException
*/
public static String readFileFromJar(String jarPath ,String name) throws IOException {
JarFile jf = new JarFile(jarPath);
Enumeration<JarEntry> jfs = jf.entries();
StringBuffer sb = new StringBuffer();
while(jfs.hasMoreElements())
{
JarEntry jfn = jfs.nextElement();
if(jfn.getName().endsWith(name))
{
InputStream is = jf.getInputStream(jfn);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buf = new byte[is.available()];
while(bis.read(buf)!=-1)
{
sb.append(new String(buf).trim());
}
bis.close();
is.close();
break;
}
}
return sb.toString();
}
java读取jar里的文件
最新推荐文章于 2025-03-13 16:38:00 发布