简体   繁体   English

如何将文件添加到指定的嵌入式JAR资源

[英]how to add file to the specified embedded JAR resource

I am using some API in which there is a class Reader which have a method readXml(String path) 我正在使用一些API,其中有一个类Reader具有方法readXml(String path)

now when i use this method and give some path which in my computer like 现在,当我使用这种方法并给出一些在我的计算机中的路径时

           Reader reader = new Reader();
           reader.readXml("C:\\work\\myfile.xml");

it always faile to read the file 它总是无法读取文件

In there API documentation it is mentioned 在API文档中提到

      readXml(java.lang.String resourcePath) 
      Loads the orthography model, from the specified embedded JAR resource.

so do i need to put my file in some embedded jar resources ,if yes what would be the way of doing it and then reading from it 所以我需要将文件放在一些嵌入式jar资源中吗,如果可以,那么从文件中读取内容的方式是什么

Thanks 谢谢

The wording is a bit strange, but I think that what the documentation means is that the resource path is a path used by the classloader to load a resource. 措辞有点奇怪,但是我认为文档的意思是资源路径是类加载器用来加载资源的路径。 The file is thus not searched in the file system, but in the classpath, following the rules detailed in ClassLoader.getResource() . 因此,遵循ClassLoader.getResource()中详述的规则,不在文件系统中而是在类路径中搜索文件。

So, the XML file should be accessible from the classpath. 因此,应该可以从类路径访问XML文件。 Suppose c:\\work is in the classpath, the resource path should thus be "myfile.xml" or "/myfile.xml" depending on how this method is implemented. 假设c:\\work在类路径中,则根据该方法的实现方式,资源路径应为"myfile.xml""/myfile.xml" If c:\\ is in the classpath, then the path should be "work/myfile.xml" or "/work/myfile.xml" . 如果c:\\在类路径中,则该路径应为"work/myfile.xml""/work/myfile.xml"

If this library is a general-purpose API and is well-designed, it should accept something other than a resource path as argument, like a java.io.Reader or java.io.InputStream. 如果此库是通用API且设计合理,则它应接受资源路径以外的其他内容作为参数,例如java.io.Reader或java.io.InputStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM