我的 Maven 资源文件夹中有一组文件:
+ src
+ main
+ resources
+ mydir
+ myfile1.txt
+ myfile2.txt
如何迭代mydir?不仅在 Eclipse 中,而且在从命令行和从依赖的 jar 运行 JUnit 测试时也是如此。
File mydir = new File("mydir");
for (File f : dir.listFiles()) {
dosomething...
}
感谢您的提示!
最佳答案
简而言之,大致:
URL pathUrl = clazz.getClassLoader().getResource("mydir/");
if ((pathURL != null) && pathUrl.getProtocol().equals("file")) {
return new File(pathUrl.toURI()).list();
}
已测试;格罗维:
def resourcesInDir(String dir) {
def ret = []
pathUrl = this.class.getClassLoader().getResource(dir)
if ((pathUrl != null) && pathUrl.getProtocol().equals("file")) {
new File(pathUrl.toURI()).list().each {
ret << "${dir}/${it}"
}
}
ret
}
files = resourcesInDir("tmp/")
files.each {
s = this.class.getResourceAsStream(it)
println s.text
}
关于java - Maven:迭代 Maven 资源目录中的文件目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8995170/