欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

从jar 包中读取文件,并且复制到jar包的目录中

程序员文章站 2022-05-04 19:53:00
...
	public static File createKillspinnersFile() {
		String path = SeleniumUtil.class.getProtectionDomain().getCodeSource()
				.getLocation().getPath();
		File file = new File(path);
		File killspinners = null;
		if (file.exists()) {
			killspinners = new File(file.isDirectory()?file:file.getParentFile(),
					"killspinners-1.1.1-fx.xpi");
			if(killspinners.isFile() && killspinners.exists()){
				return killspinners;
			}
		}else{
			log.error("localtion path is not exists");
		}
		URL plugin = SeleniumUtil.class
				.getResource("/plugin/killspinners-1.1.1-fx.xpi");
		InputStream in = null;
		try {
			in = plugin.openStream();
			FileUtils.writeByteArrayToFile(killspinners, IOUtils.toByteArray(in));
			log.error("createKillspinnersFile succeed");
		}catch (Exception e) {
			log.error(e.getMessage(), e);
		}finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
			}
		}
		return killspinners;

	}