java 读取jar 文件里的xml文件
2017-11-10 本文已影响75人
sirai
新建一个类 写一个main方法
private static final String logback = "district.xml";
static {
//读取文件
}
public static void main(String[] args){
String path = ProvinceVo.class.getProtectionDomain().getCodeSource().getLocation().getPath();
System.out.println(path);
File logbackFile = new File(logback);
if (!logbackFile.exists()) {
try {
FileUtil.readJarFile(path, logback);
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.新建一个file 文件
public class FileUtil {
/**
* 读取jar包文件
* @param jarPath
* @param filename
* @throws IOException
*/
public static void readJarFile(String jarPath, String filename) throws IOException {
JarFile jarFile = new JarFile(jarPath);
StringBuffer sb=new StringBuffer();
InputStreamReader input = new InputStreamReader( jarFile.getInputStream(jarFile.getJarEntry(filename)));
BufferedReader reader = new BufferedReader(input);
String line;
while ((line = reader.readLine()) != null) {
sb.append(line.trim());
}
System.out.println(sb.toString());
reader.close();
jarFile.close();
}
}
3.执行 mvn clean package命令
4.首先cd 到jar包所在的目录
图片.png
5.执行main 方法
java -cp xcommons-util-1.0.jar com.xproject.xcommons.vo.ProvinceVo`