use*_*877 107
最后,我找到了解决方案:
final String path = "sample/folder";
final File jarFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
if(jarFile.isFile()) { // Run with JAR file
final JarFile jar = new JarFile(jarFile);
final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
while(entries.hasMoreElements()) {
final String name = entries.nextElement().getName();
if (name.startsWith(path + "/")) { //filter according to the path
System.out.println(name);
}
}
jar.close();
} else { // Run with IDE
final URL url = Launcher.class.getResource("/" + path);
if (url != null) {
try {
final File apps = new File(url.toURI());
for (File app : apps.listFiles()) {
System.out.println(app);
}
} catch (URISyntaxException ex) {
// never happens
}
}
}
Run Code Online (Sandbox Code Playgroud)
第二个块只在IDE上运行应用程序时工作(不是使用jar文件),如果不喜欢,可以删除它.
Gle*_*est 15
请尝试以下方法. 如果您的类路径是com.abc.package.MyClass并且您
的资源"<PathRelativeToThisClassFile>/<ResourceDirectory>"文件位于src/com/abc/package/resources /中,请创建资源路径Eg:
URL url = MyClass.class.getResource("resources/");
if (url == null) {
// error - missing folder
} else {
File dir = new File(url.toURI());
for (File nextFile : dir.listFiles()) {
// Do something with nextFile
}
}
Run Code Online (Sandbox Code Playgroud)
你也可以使用
URL url = MyClass.class.getResource("/com/abc/package/resources/");
Run Code Online (Sandbox Code Playgroud)
hum*_*ngV 13
以下代码将所需的“文件夹”作为路径返回,无论它是否位于 jar 内。
private Path getFolderPath() throws URISyntaxException, IOException {
URI uri = getClass().getClassLoader().getResource("folder").toURI();
if ("jar".equals(uri.getScheme())) {
FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap(), null);
return fileSystem.getPath("path/to/folder/inside/jar");
} else {
return Paths.get(uri);
}
}
Run Code Online (Sandbox Code Playgroud)
需要 java 7+。
小智 7
我知道这是多年前的事了.但只是为了其他人遇到这个话题.你可以做的是使用getResourceAsStream()带有目录路径的方法,输入Stream将拥有该目录中的所有文件名.之后,您可以使用每个文件名连接dir路径,并为循环中的每个文件调用getResourceAsStream.
当我试图从jar中包装的资源加载一些hadoop配置时,我在IDE和jar(发布版本)上遇到了同样的问题.
我发现java.nio.file.DirectoryStream最好在本地文件系统和jar上迭代目录内容.
String fooFolder = "/foo/folder";
....
ClassLoader classLoader = foofClass.class.getClassLoader();
try {
uri = classLoader.getResource(fooFolder).toURI();
} catch (URISyntaxException e) {
throw new FooException(e.getMessage());
} catch (NullPointerException e){
throw new FooException(e.getMessage());
}
if(uri == null){
throw new FooException("something is wrong directory or files missing");
}
/** i want to know if i am inside the jar or working on the IDE*/
if(uri.getScheme().contains("jar")){
/** jar case */
try{
URL jar = FooClass.class.getProtectionDomain().getCodeSource().getLocation();
//jar.toString() begins with file:
//i want to trim it out...
Path jarFile = Paths.get(jar.toString().substring("file:".length()));
FileSystem fs = FileSystems.newFileSystem(jarFile, null);
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(fs.getPath(fooFolder));
for(Path p: directoryStream){
InputStream is = FooClass.class.getResourceAsStream(p.toString()) ;
performFooOverInputStream(is);
/** your logic here **/
}
}catch(IOException e) {
throw new FooException(e.getMessage());
}
}
else{
/** IDE case */
Path path = Paths.get(uri);
try {
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path);
for(Path p : directoryStream){
InputStream is = new FileInputStream(p.toFile());
performFooOverInputStream(is);
}
} catch (IOException _e) {
throw new FooException(_e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
130815 次 |
| 最近记录: |