简体   繁体   English

无法从类路径获取文件(使用NIO2)

[英]Cann't get file from classpath (using NIO2)

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

(As I understand this is a path of new NIO2 API, that is a part of Java 7.) (据我了解,这是新的NIO2 API的路径,它是Java 7的一部分。)

stringTemplatePath parameter is a name of the file ( "template.html" ). stringTemplatePath参数是文件的名称( “ template.html” )。 I check location of this file. 我检查此文件的位置。 It is in the classpath: ../classes/template.html 它在类路径中: ../classes/template.html

After calling this function I get an exception: 调用此函数后,出现异常:

java.nio.file.NoSuchFileException: template.html

Maybe I send filename parameter to in a wrong way? 也许我以错误的方式将filename参数发送给? I tried to send this modification: "file:///template.html" and "classpath:template.html" , but it didn't help. 我试图发送此修改: “ file:///template.html”“ classpath:template.html” ,但没有帮助。

Also I tried this code: 我也尝试了这段代码:

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

So, file in classpath because new File(stringTemplatePath) can create a File. 因此,将文件放在类路径中,因为新的File(stringTemplatePath)可以创建一个文件。 But this file has very strange path ( /opt/repo/versions/8.0.9/temp/template.html ). 但是此文件具有非常奇怪的路径( /opt/repo/versions/8.0.9/temp/template.html )。 I use Jelastic as hosting (enviroment: Java 8, Tomcat 8), if it is metter. 我会使用Jelastic作为托管服务器(环境:Java 8,Tomcat 8)。


UPDATE: FINAL WORKING SOLUTION: 更新:最终工作解决方案:

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

IOUtils is util class from Apache IO Commons. IOUtils是Apache IO Commons的util类。

Impotant note: 重要提示:

If I just invoke .getResourceAsStream(...) from class , resource file will be not find and method will return null : 如果我只是从类中调用.getResourceAsStream(...) ,将找不到资源文件,方法将返回null

MyClass.class.getResourceAsStream(stringTemplatePath);

So, I call .getClassLoader() before calling .getResourceAsStream(...) and it works perfectly: 所以,我呼吁.getResourceAsStream(...)之前调用.getClassLoader()和它完美的作品:

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

You should not be trying and accessing resources in your classpath as Path s. 您不应该尝试使用Path访问类Path的资源。

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; 当您的项目位于IDE设置中时,这很可能会起作用,但是,一旦将项目打包为jar,就不会立即生效。 it is then impossible to access them using even Path (and even though you can open zip files, therefore jars, as FileSystem s). 这样就无法使用甚至Path来访问它们(即使您可以打开zip文件,因此也可以将jars作为FileSystem来打开)。

Use the dedicated methods to do that instead, starting with .getResourceAsStream() : .getResourceAsStream()开始,使用专用方法来做到这一点:

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

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). 请注意,您将需要检查该方法的返回码是否为null (如果在类路径中找不到资源,则返回此值)。

If the file is really part of the classpath you should use: ClassName.class.getResourceAsStream("/file name") this return InputStraem 如果文件确实是类路径的一部分,则应使用: ClassName.class.getResourceAsStream("/file name")此返回InputStraem
or 要么
ClassName.class.getResource("/file name") this return URL ClassName.class.getResource("/file name")此返回URL

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

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