gpt4 book ai didi

java - Maven:迭代 Maven 资源目录中的文件目录

转载 作者:行者123 更新时间:2023-11-29 06:04:28 26 4
gpt4 key购买 nike

我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com