无法从类路径获取文件(使用NIO2) [英] Cann't get file from classpath (using NIO2)

查看:187
本文介绍了无法从类路径获取文件(使用NIO2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文件内容创建一个String。根据这个答案,我这样做:

I want to create a String from the content of the file. According this answer I do it in this way:

private static String buildStringFromTemplate(String stringTemplatePath) throws IOException {
    byte[] encoded = Files.readAllBytes(Paths.get(stringTemplatePath));
    return new String(encoded, "UTF-8");
}

(据我所知,这是新NIO2 API的路径,这是Java 7的一部分。)

stringTemplatePath 参数是文件的名称(模板。 HTML)。我检查这个文件的位置。它位于类路径中: ../ classes / template.html

stringTemplatePath parameter is a name of the file ("template.html"). I check location of this file. It is in the classpath: ../classes/template.html

调用此函数后,我得到一个异常:

After calling this function I get an exception:

java.nio.file.NoSuchFileException: template.html

也许我以错误的方式发送文件名参数?我尝试发送此修改:file:///template.htmlclasspath:template.html,但它没有帮助。

Maybe I send filename parameter to in a wrong way? I tried to send this modification: "file:///template.html" and "classpath:template.html", but it didn't help.

我也试过这段代码:

private static String buildStringFromTemplate(String stringTemplatePath) throws IOException {
    File file = new File(stringTemplatePath);
    String absolutePath = file.getAbsolutePath();
    byte[] encoded = Files.readAllBytes(Paths.get(absolutePath));
    return new String(encoded, "UTF-8");
}

我调用了这个函数我得到以下异常:

I called this function I get following exception:

java.nio.file.NoSuchFileException: /opt/repo/versions/8.0.9/temp/template.html

因此,在classpath中提交文件,因为新文件(stringTemplatePath)可以创建文件。但是这个文件有很奇怪的路径( /opt/repo/versions/8.0.9/temp/template.html )。我使用Jelastic作为托管(环境:Java 8,Tomcat 8),如果它是metter。

So, file in classpath because new File(stringTemplatePath) can create a File. But this file has very strange path (/opt/repo/versions/8.0.9/temp/template.html). I use Jelastic as hosting (enviroment: Java 8, Tomcat 8), if it is metter.

UPDATE :最终工作解决方案:

private static String buildStringFromTemplate(String stringTemplatePath) throws IOException {
    InputStream inputStream = MyClass.class.getClassLoader().getResourceAsStream(stringTemplatePath);
    return IOUtils.toString(inputStream, "UTF-8"); 
}

IOUtils 是来自Apache IO Commons的util类。

IOUtils is util class from Apache IO Commons.

重要注意事项:

如果我只是调用 .getResourceAsStream(来自 class 的<)>,资源文件将无法找到,方法将返回 null

MyClass.class.getResourceAsStream(stringTemplatePath);

所以,在调用之前,我调用 .getClassLoader()。 getResourceAsStream(...)并且它完美运行:

So, I call .getClassLoader() before calling .getResourceAsStream(...) and it works perfectly:

MyClass.class.getClassLoader().getResourceAsStream(stringTemplatePath);


推荐答案

您不应该尝试并访问类路径中的资源as Path s。

You should not be trying and accessing resources in your classpath as Paths.

虽然当你的项目处于IDE设置中时,这很可能会有效,但它赢了t一旦你的项目打包成一个罐子;然后甚至无法使用 Path 访问它们(即使你可以打开zip文件,因此jars,因为 FileSystem s)。

While this will very probably work when your project sits in your IDE setup, it won't as soon as your project is packaged as a jar; it is then impossible to access them using even Path (and even though you can open zip files, therefore jars, as FileSystems).

使用专用方法代替,以 .getResourceAsStream()开头:

Use the dedicated methods to do that instead, starting with .getResourceAsStream():

final InputStream in = MyClass.class.getResourceAsStream("/path/to/resource");

请注意,您需要检查该方法的返回码是否为 null (如果在类路径中找不到资源,则返回此内容。)

Note that you will need to check whether the return code of that method is null (this is what is returned if the resource is not found in the classpath).

这篇关于无法从类路径获取文件(使用NIO2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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