[英]How to check existence of xml file in resources folder in java?
I wanan check is the resources folder has a xml with the given name.我想检查资源文件夹是否有一个 xml 和给定的名称。 I can do by using File class and then check exists() method, but I was wondering if I can refer to resource file as a Resource and not by File?
我可以通过使用文件 class 然后检查 exists() 方法来完成,但我想知道我是否可以将资源文件称为资源而不是文件? I tried following but it does not seem to work:
我试过以下但它似乎不起作用:
Resource resource = new ClassPathResource("resources/folder/test.xml");//this file is existing
if(resource.exists())
{
System.out.println(" SUCCESS ");
}
else
{
System.out.println(" FAIL . RESOURCE NOT FOUND");
}
Its always going else block when this piece of code is executed.What did I miss?执行这段代码时,它总是会阻塞。我错过了什么?
The src/main/resources
is the root of the classpath (or the files in there are added to the root. So resources/folder/test.xml
would expect a file in src/main/resources/resources/folder/test.xml
. This is probably not what you want. Remove the resources
part in the file path. src/main/resources
是类路径的根目录(或者其中的文件被添加到根目录中。因此resources/folder/test.xml
会期望src/main/resources/resources/folder/test.xml
的文件。这可能不是你想要的。删除文件路径中的resources
部分。
Resource resource = new ClassPathResource("/folder/test.xml");
if(resource.exists()) {
System.out.println(" SUCCESS ");
} else {
System.out.println(" FAIL . RESOURCE NOT FOUND");
}
Should do the trick.应该做的伎俩。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.