Java 资源加载:如何确定它是否';这是一个目录

Java 资源加载:如何确定它是否';这是一个目录,java,jar,resource-loading,Java,Jar,Resource Loading,目前我使用此解决方案加载资源: URL url = MyClass.class.getClassLoader().getResource("documents/"+path); if(url == null) throw new FileNotFoundException(); BufferedReader reader = new BufferedReader( new InputStreamReader(url.openStream())); 遗憾的是,我无法控制path

目前我使用此解决方案加载资源:

URL url = MyClass.class.getClassLoader().getResource("documents/"+path);
if(url == null)
    throw new FileNotFoundException();

BufferedReader reader = new BufferedReader(
    new InputStreamReader(url.openStream()));

遗憾的是,我无法控制
path
是文件还是目录。是否有任何方法可以确定所表示的路径是否为目录?我正在寻找一种独立于资源加载位置的解决方案(换句话说:从JAR加载资源时不起作用)。

我使用此代码从JAR获取文本文件的名称

public String[] getFiles() throws IOException {
    ArrayList<String> list = new ArrayList<String>();
    List<JarEntry> ents = new ArrayList<JarEntry>();
    Enumeration<JarEntry> e = null;

    URL jarp = getLocation();
    if (jarp != null) {
        jar = jarp.getProtocol().equalsIgnoreCase("jar") ? jarp : new URL("jar:" + jarp.toString() + "!/");
        JarFile jarf = null;
        try {
            jarf = AccessController.doPrivileged(
                    new PrivilegedExceptionAction<JarFile>() {

                        @Override
                        public JarFile run() throws Exception {
                            JarURLConnection conn = (JarURLConnection) jar.openConnection();
                            conn.setUseCaches(false);
                            return conn.getJarFile();
                        }
                    });
        } catch (PrivilegedActionException ex) {
            Logger.getLogger(LicenseLoader.class.getName()).log(Level.SEVERE, null, ex);
        }
        e = jarf.entries();
        while (e.hasMoreElements()) {
            JarEntry je = e.nextElement();
            if (!je.isDirectory()) {
                ents.add(je);
            }
        }
        for (JarEntry ent : ents) {
            if ((ent.getName().startsWith(pathName)) && (ent.getName().endsWith(".txt"))) {
                String name = ent.getName().replace(pathName, "");
                list.add(name);
            }
        }
    }
    return list.toArray(new String[list.size()]);
}
public String[]getFiles()引发IOException{
ArrayList=新建ArrayList();
列表元素=新的ArrayList();
枚举e=null;
URL jarp=getLocation();
if(jarp!=null){
jar=jarp.getProtocol().equalsIgnoreCase(“jar”)?jarp:newURL(“jar:+jarp.toString()+”!/”;
JarFile jarf=null;
试一试{
jarf=AccessController.doPrivileged(
新PrivilegedExceptionAction(){
@凌驾
public JarFile run()引发异常{
JarURLConnection conn=(JarURLConnection)jar.openConnection();
conn.SETUSECHACHES(假);
返回conn.getJarFile();
}
});
}捕获(PrivilegedActionException ex){
Logger.getLogger(LicenseLoader.class.getName()).log(Level.SEVERE,null,ex);
}
e=jarf.entries();
而(e.hasMoreElements()){
JarEntry je=e.nextElement();
如果(!je.isDirectory()){
添加(日本脑炎);
}
}
对于(Jarent:ents){
if((ent.getName().startsWith(pathName))&&(ent.getName().endsWith(“.txt”)){
String name=ent.getName().replace(路径名“”);
列表。添加(名称);
}
}
}
return list.toArray(新字符串[list.size()]);
}