[英]Read file on target/test-classes FileNotFound
I´m reading a file from my resource folder in a unit test 我正在单元测试中从资源文件夹中读取文件
I can see the file in the target/test-classes 我可以在目标/测试类别中看到文件
But when I try to wrap the file into a fileInputStream this one throw the exception because cannot find the file 但是当我尝试将文件包装到fileInputStream中时,这会引发异常,因为找不到文件
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("CV.pdf").getFile());
FileInputStream fis = new FileInputStream(file)
Output debuging: 输出调试:
classLoader.getResource("CV.pdf"): classLoader.getResource(“ CV.pdf”):
file:/Users/Development/File%20-%20Test%20Java%20trial/Code/target/test-classes/CV.pdf
Maybe it could be because my folder has whitespace names? 也许是因为我的文件夹有空格名称?
The file is there for sure, I can seen it in that folder. 该文件肯定存在,我可以在该文件夹中看到它。
What I´m doing wrong here?. 我在这里做错了什么?
Regards. 问候。
Do you really need FileInputStream ? 您真的需要FileInputStream吗?
Actual file can be even inside jar (if you instruct maven to make test-jar). 实际文件甚至可以放在jar中(如果您指示Maven创建test-jar)。
More flexible way is to use InputStream instead: 更灵活的方法是改用InputStream :
try(final InputStream is = getClass().getResource("CV.pdf").openStream())
{
//Do something with is
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.