I have a file in my resources folder in my project in Eclipse. I need a way in which to load this document into my Java file. Preferable representation would be in an InputStream.
我在Eclipse项目中的资源文件夹中有一个文件。我需要一种方法将此文档加载到我的Java文件中。优先表示将在InputStream中。
I tried the following based on some searching but it does not seem to be working and I am not sure why (I get null), any help appreciated
我根据一些搜索尝试了以下但它似乎没有工作,我不知道为什么(我得到null),任何帮助赞赏
InputStream is = getClass().getResourceAsStream("/Project/resources/BlankPDF.pdf");
2 个解决方案
#1
0
The string which you pass as an argument to getResourceAsStream
has to be either the location relative to the class you call getResourceAsStream
from, or absolute w.r.t. any of your source folders, with a leading /
.
作为getResourceAsStream的参数传递的字符串必须是相对于您调用getResourceAsStream的类的位置,或绝对值w.r.t.您的任何源文件夹,带有前导/。
So if resources
is a source folder in your project (marked with a little package-like symbol in Eclipse) then getClass().getResourceAsStream("/BlankPDF.pdf")
should work.
因此,如果资源是项目中的源文件夹(在Eclipse中标有一个类似于包的符号),那么getClass()。getResourceAsStream(“/ BlankPDF.pdf”)应该可以工作。
Example: Say you have the following folders and packages in your project, source folders being marked with a *
:
示例:假设您的项目中包含以下文件夹和包,源文件夹标有*:
Project
src*
com.package.foo
MyClass.java
resources*
BlankPDF.pdf
Then both MyClass.class.getResourceAsStream("/BlankPDF.pdf")
(leading /
, absolute) and MyClass.class.getResourceAsStream("../../../BlankPDF.pdf")
(no leading /
, relative) should get you the file.
然后是MyClass.class.getResourceAsStream(“/ BlankPDF.pdf”)(前导/,绝对)和MyClass.class.getResourceAsStream(“../../../ BlankPDF.pdf”)(无前导/,相对)应该得到你的文件。
#2
0
Ensure the resource is located in the same folder as your compiled classes. I don't use Eclipse, but Netbeans automatically creates a "classes" folder upon build. Classes that are in a package, i.e. org.website.PDFReader
, will have a set of subdirectories under the 'classes' folder, following the fully qualified name of the class (i.e. package name + class name). Regardless, the 'parent directory' for these packaged classes is still the 'classes' folder.
确保资源与编译的类位于同一文件夹中。我不使用Eclipse,但Netbeans会在构建时自动创建“classes”文件夹。包中的类,即org.website.PDFReader,将在“classes”文件夹下具有一组子目录,遵循类的完全限定名称(即包名+类名)。无论如何,这些打包类的“父目录”仍然是“类”文件夹。
For the time being, place your resources in that classes folder manually. There is almost certainly a way to make Eclipse automatically copy resources into the appropriate folder upon build (see below).
目前,请手动将资源放在该类文件夹中。几乎可以肯定有一种方法可以让Eclipse在构建时自动将资源复制到相应的文件夹中(参见下文)。
Finally, in your code, remove the leading "/" from "/Project/..."
最后,在您的代码中,从“/ Project / ...”中删除前导“/”
When you remove that leading "/", it makes the resource location relative to the parent directory of your Class's class loader - in this example, the 'classes' folder. Keeping that forward-slash makes the resource location 'absolute' - not relative to any parent directory.
删除前导“/”时,它会使资源位置相对于类的类加载器的父目录 - 在本例中为'classes'文件夹。保持正斜杠使资源位置“绝对” - 不相对于任何父目录。
Edit: The following question should help out with the more Eclipse-specific details of this process: Java in Eclipse: Where do I put files on the filesystem that I want to load using getResource? (e.g. images for an ImageIcon). Make sure to check out the first comment on the accepted answer, as well.
编辑:以下问题应该有助于解决此过程中更多特定于Eclipse的详细信息:Eclipse中的Java:我在哪里将文件放在要使用getResource加载的文件系统上? (例如ImageIcon的图像)。请务必查看已接受答案的第一条评论。
#1
0
The string which you pass as an argument to getResourceAsStream
has to be either the location relative to the class you call getResourceAsStream
from, or absolute w.r.t. any of your source folders, with a leading /
.
作为getResourceAsStream的参数传递的字符串必须是相对于您调用getResourceAsStream的类的位置,或绝对值w.r.t.您的任何源文件夹,带有前导/。
So if resources
is a source folder in your project (marked with a little package-like symbol in Eclipse) then getClass().getResourceAsStream("/BlankPDF.pdf")
should work.
因此,如果资源是项目中的源文件夹(在Eclipse中标有一个类似于包的符号),那么getClass()。getResourceAsStream(“/ BlankPDF.pdf”)应该可以工作。
Example: Say you have the following folders and packages in your project, source folders being marked with a *
:
示例:假设您的项目中包含以下文件夹和包,源文件夹标有*:
Project
src*
com.package.foo
MyClass.java
resources*
BlankPDF.pdf
Then both MyClass.class.getResourceAsStream("/BlankPDF.pdf")
(leading /
, absolute) and MyClass.class.getResourceAsStream("../../../BlankPDF.pdf")
(no leading /
, relative) should get you the file.
然后是MyClass.class.getResourceAsStream(“/ BlankPDF.pdf”)(前导/,绝对)和MyClass.class.getResourceAsStream(“../../../ BlankPDF.pdf”)(无前导/,相对)应该得到你的文件。
#2
0
Ensure the resource is located in the same folder as your compiled classes. I don't use Eclipse, but Netbeans automatically creates a "classes" folder upon build. Classes that are in a package, i.e. org.website.PDFReader
, will have a set of subdirectories under the 'classes' folder, following the fully qualified name of the class (i.e. package name + class name). Regardless, the 'parent directory' for these packaged classes is still the 'classes' folder.
确保资源与编译的类位于同一文件夹中。我不使用Eclipse,但Netbeans会在构建时自动创建“classes”文件夹。包中的类,即org.website.PDFReader,将在“classes”文件夹下具有一组子目录,遵循类的完全限定名称(即包名+类名)。无论如何,这些打包类的“父目录”仍然是“类”文件夹。
For the time being, place your resources in that classes folder manually. There is almost certainly a way to make Eclipse automatically copy resources into the appropriate folder upon build (see below).
目前,请手动将资源放在该类文件夹中。几乎可以肯定有一种方法可以让Eclipse在构建时自动将资源复制到相应的文件夹中(参见下文)。
Finally, in your code, remove the leading "/" from "/Project/..."
最后,在您的代码中,从“/ Project / ...”中删除前导“/”
When you remove that leading "/", it makes the resource location relative to the parent directory of your Class's class loader - in this example, the 'classes' folder. Keeping that forward-slash makes the resource location 'absolute' - not relative to any parent directory.
删除前导“/”时,它会使资源位置相对于类的类加载器的父目录 - 在本例中为'classes'文件夹。保持正斜杠使资源位置“绝对” - 不相对于任何父目录。
Edit: The following question should help out with the more Eclipse-specific details of this process: Java in Eclipse: Where do I put files on the filesystem that I want to load using getResource? (e.g. images for an ImageIcon). Make sure to check out the first comment on the accepted answer, as well.
编辑:以下问题应该有助于解决此过程中更多特定于Eclipse的详细信息:Eclipse中的Java:我在哪里将文件放在要使用getResource加载的文件系统上? (例如ImageIcon的图像)。请务必查看已接受答案的第一条评论。