java 读取jar包中的文件

参考: https://blog.csdn.net/rchm8519/article/details/39557499

参考: https://www.cnblogs.com/zeciiii/p/4178824.html

 需求:

我自己写了一个Java 的记事本工具,然后实现快捷键功能,于是就把快捷键对应的key-value 保存到properties文件中,

类似:

Ctrl,S=\u4FDD\u5B58
Ctrl,P=\u53E6\u5B58\u4E3A
Ctrl,N=\u65B0\u5EFA
Ctrl,Esc=\u9000\u51FA

问题:

但是发现,当将项目打成jar 包以后,在jar 包内部读取jar包内部的文件,一直都行不通,因为路径问题,从外部读取jar包中的内容,可以通过: ...jar!/文件路径,找到内容,但是自己本身读取自己,就会总是FileNotFoundException ,后来发现有个专门读取jar包内容的JarFile

从外部读取jar包内容实例:

public static Map<String, String> init(String defaultPath) {
		if(CheckUtil.empty(defaultPath)){
			defaultPath = ReadProperties.defaultPath;
		}
		String packagePath = "/file/"+defaultPath;
		URL resource = ReadProperties.class.getResource(packagePath);//根据 source 文件下的包路径,得到url,必须是绝对路径
		String path = resource.getFile();//根据url 得到完整的 路径
		PropertiesParse parse = new PropertiesParse();
		InputStream input = null;
		if(path.contains(".jar")) {
			//得到jar目录
			String jarPath = path.substring(0, path.indexOf("!"));
			//得到jarFile 对象
			JarFile jarFile = null;
			try {
				jarFile = new JarFile(jarPath);
			} catch (IOException e) {
				e.printStackTrace();
			}
			ZipEntry entry = jarFile.getEntry(packagePath.substring(1));//这里是相对路径,前面没有斜杠/
			try {
				input = jarFile.getInputStream(entry);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}else{//正常class 读取,普通File 读取
			try {
				input = new FileInputStream(new File(path));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		parse.setSrcToReader(input);
		Map<String, String> map = parse.getMap();
		
		return map;
	}
	
	public static void main(String[] args) {
		String defaultPath = "";
		String packagePath = "/file/"+"keyEvent.properties";
		// 读取jar包中的文件,参考地址; https://blog.csdn.net/rchm8519/article/details/39557499,https://www.cnblogs.com/zeciiii/p/4178824.html
		String str = "/C:/Users/12198/Desktop/KEditPad.jar";// /file/keyEvent.properties
		JarFile jarFile = null;
		PropertiesParse parse = new PropertiesParse();
		InputStream input = null;
		try {
			jarFile = new JarFile(str);//通过jarFile  读取jar包
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//遍历entry 对象
		Enumeration<JarEntry> entries = jarFile.entries();
		while(entries.hasMoreElements()){
			System.out.println(entries.nextElement().getName());
		}
		ZipEntry entry = jarFile.getEntry(packagePath.substring(1));//name 最开头没有/,换句话就是相对路径
		try {
			input = jarFile.getInputStream(entry);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		parse.setSrcToReader(input);
		Map<String, String> map = parse.getMap();
		
		
	}
	

得到了文件路径,通过File 的new File(str),会报FileNotFound 异常,java 读取jar包中的文件,应该用JarFile 类来读取

jar包内部读取jar包内容实例:(外部或者项目中也可通过此方式)

如果file放在源码目录(例如:我把文件放在src下的file包下)下,则无论是在外部读取还是在jar包中读取,都可通过classLoader

public static Map<String, String> init(String defaultPath) {
		 InputStream stream =  
                 ClassLoader.getSystemResourceAsStream("file/keyEvent.properties");
		 PropertiesParse parse = new PropertiesParse();
		 parse.setSrcToReader(stream);
			Map<String, String> map = parse.getMap();
		return map;
		
	}

猜你喜欢

转载自blog.csdn.net/kzcming/article/details/82346817