0

我有一个独立的应用程序,我将其导出为 JAR 文件,以将其包含在 Tomcat 上运行的 Web 应用程序中。

独立应用程序通过以下行从包“config”中的文件读取:

configFileName = Thread.currentThread().getContextClassLoader().getResource("config/indexer.cfg").getPath();

不幸的是,这会导致 Web 应用程序出现异常:

java.io.FileNotFoundException: file:\C:\apache-tomcat-7.0.23\IRSimWebApp\WEB-INF\lib\IR_Sim.jar!\config\indexer.cfg (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)

我注意到文件路径中有一个感叹号。这会导致错误吗?它是如何到达那里的?

4

1 回答 1

1

将资源获取为以下内容似乎会更好地为您服务InputStream

InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("config/indexer.cfg");
if (in != null) {
    BufferedInputStream buff = new BufferedInputStream(in);
    // process buff to get contents of file
    buff.close();
}

显然,上面没有异常处理,但你应该明白......

于 2013-02-20T18:38:27.297 回答