从外部Jar文件中读取文件? [英] Read a file from inside of an external Jar file?

查看:127
本文介绍了从外部Jar文件中读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用java读取外部jar文件中的文件。
例如,我有两个jar文件。一个是foo.jar,另一个是bar.jar。 bar.jar里面是文件foo-bar.txt。如何使用foo.jar中的代码从bar.jar中读取文件foo-bar.txt...?这甚至可能..?
我知道我可以从foo.jar中读取一个文件,使用

I'm trying to read a file from inside an external jar using java.. For example, I have two jar files. One is "foo.jar" the other is "bar.jar". Inside of "bar.jar" is the file "foo-bar.txt". How do i read the file "foo-bar.txt" from inside of "bar.jar" using code in "foo.jar"...? Is this even possible..? I know that i can read a file from iside of foo.jar using

this.getClass().getClassLoader().getResourceAsStream("foo-bar.txt");

但是我不知道如何从外部的jar里面执行。有人可以帮我吗?

But I don't know how to do it from an external jar.. can someone help me?

推荐答案

InputStream in = null;
String inputFile = "jar:file:/c:/path/to/my.jar!/myfile.txt";
if (inputFile.startsWith("jar:")){
  try {
    inputURL = new URL(inputFile);
    JarURLConnection conn = (JarURLConnection)inputURL.openConnection();
    in = conn.getInputStream();
  } catch (MalformedURLException e1) {
    System.err.println("Malformed input URL: "+inputURL);
    return;
  } catch (IOException e1) {
    System.err.println("IO error open connection");
    return;
  }
} 

这篇关于从外部Jar文件中读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆