Java "File" works but "InputStream" does not -


edit: getresourceasstream() vs fileinputstream

is best explanation have found. after trying different subclasses of inputstream feel implementation of inputstream returned fileinputstream() , 1 returned getresourceasstream() subtly different in way (causing incompatibility javax.xml.parsers.. )

i'll leave open while in case happens have answer, i'm done. advice, suggestions , help. time move on next thing.


i've have servlet collects it's initialization data xml ini file, opens inputstream servlet session context this

   httpsession session = request.getsession(true);    servletcontext ctx=session.getservletcontext();    inputstream myini = ctx.getresourceasstream("/web-inf/myini.xml"); 

that works, later on i'm writing junit tests , in setup function, , need access same ini. don't have servlet context in test environment try create inputstream using

inputstream testing = new fileinputstream(string pathtofile); 

and also

inputstream testing = new fileinputstream(file fileobj);  

the exception thrown xml parser (below)

i find impossible inputstream init file , forced use file.

i checked javadoc's, moved file location in case of security restrictions. added dual constructors classes needing ini, accept both file , inputstream cater unit testing (where need file ref)and runtime (where inputstream returned servlet session context) ...

but i'm perplexed/pretty frustrated have ask

how come can return file object using "file", can parsed javax.xml.parsers (see function below)

file myini = new file("c:\\apache-tomcat-7.0.30\\myini\\myini.xml"); 

but cannot same using "inputstream" ?

inputstream myini = new fileinputstream("c:\\apache-tomcat-7.0.30\\myini\\myini.xml"); 

using same string path (i.e. file exists)

respectively each gets passed either

public xmlnode parse(inputstream is) throws xmlexception {         try {             documentbuilderfactory dbfactory = documentbuilderfactory.newinstance();             documentbuilder dbuilder = dbfactory.newdocumentbuilder();             document = dbuilder.parse(is);             document.getdocumentelement().normalize();              xmlnode node = new xmlnode(document.getdocumentelement());             return node;         } catch (parserconfigurationexception e) {             throw new xmlexception("error in configuration of xml parser", e);         } catch (saxexception e) {             throw new xmlexception("error in parsing xml document", e);         } catch (ioexception e) {             throw new xmlexception("error in reading inputstream", e);         }     } 

or

public xmlnode parse(file file) throws xmlexception {         try {             documentbuilderfactory dbfactory = documentbuilderfactory.newinstance();             documentbuilder dbuilder = dbfactory.newdocumentbuilder();             document = dbuilder.parse(file);             document.getdocumentelement().normalize();              xmlnode node = new xmlnode(document.getdocumentelement());             return node;         } catch (parserconfigurationexception e) {             throw new xmlexception("error in configuration of xml parser", e);         } catch (saxexception e) {             throw new xmlexception("error in parsing xml document", e);         } catch (ioexception e) {             throw new xmlexception("error in opening file", e);         }     } 

this exception gets thrown when inputstream method called (after appropriate initialization above)

xml.utils.xmlexception: error in reading inputstream     @ xml.utils.xmldocument.parse(xmldocument.java:40)     @ com.jcando.util.xmlini.<init>(xmlini.java:49) 

is there different way of defining path string inputstream ? there security block i'm unaware of?

if can explain missing, or being thick stump i'd appreciate it.

you can write file f = new file("anyname"): never throw exception if file not exist.

yet, writing inputstream myini = new fileinputstream("anyname") throw filenotfoundexception exception if file not exist can see in documentation.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -