java - Resource Loading: How to determine if it's a directory -
currently use solution load resources:
url url = myclass.class.getclassloader().getresource("documents/"+path); if(url == null) throw new filenotfoundexception(); bufferedreader reader = new bufferedreader( new inputstreamreader(url.openstream()));
sadly can't control whether path
file or directory. there way can determine if denoted path directory? i'm looking solution independent resource loaded (in other words: file.isfile
won't work when loading resource jar).
i use code grab names of text files 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); } } (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()]); }
Comments
Post a Comment