Read a file from inside of an external Jar file?

后端 未结 2 1620
天涯浪人
天涯浪人 2021-01-07 00:16

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

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-07 00:20

    Use jar url to open connection the example code

    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;
      }
    } 
    

提交回复
热议问题