java - IOException, not finding file -
java - IOException, not finding file -
i hoping explain why not work , solution might be.
i have tried next larn happens:
string s = "\\users\\udc8\\a4471\\my documents\\matlab\\blockdiagram.xml"; string st = "\\"; string st2 = st + s; system.out.println(st2);
giving me next output:
\\users\udc8\a4471\my documents\matlab\blockdiagram.xml
which right path file. seek parse file using sax , ioexception saying file not exist. have tried using file , getpath(),getcanonicalpath() , getabsolutepath(). when running parser msg:
due ioexception, parser not check \\users\udc8\a4471\my documents\matlab\\blockdiagram.xml
this code starting parsing:
try { xmlreader parser = xmlreaderfactory.createxmlreader(); parser.parse(st2); system.out.println(s + " well-formed."); } grab (saxexception e) { system.out.println(s + " not well-formed."); } grab (ioexception e) { system.out.println( "due ioexception, parser not check " + s ); }
running similar programme not have own messege next error messege returned:
java.io.filenotfoundexception: \\users\udc8\a4471\workspace\sax intro\users\udc8\a4471\my documents\matlab\blockdiagram.xml (the scheme cannot find file specified)
the file has no special restrictions (as far can see), nil ticked in looking @ file properties , can manually re-write content. ideas?
maybe because of path has 2 \-signs much in it. recomend give seek variable s instead of s2 has additional \-signs added.
to honest realized how less know paths in java, expecially when comes different os.
i managed run on windows machine so:
import java.io.file; import java.io.ioexception; import org.xml.sax.saxexception; import org.xml.sax.xmlreader; import org.xml.sax.helpers.xmlreaderfactory; public class saxsample { public static void main(string[] args) { new saxsample().run(); } public void run() { system.out.println("this class located under: "+getabsolutepath()); // using absolut pathd string absolutpath = "d:\\temp\\oki.xml"; file f = new file(absolutpath); system.out.println("does file exist using absolut path? -> "+f.exists()); runsax(absolutpath); // using relative path (i dont know why knows drive c:/, d:/ take .class running same drive .xml in) string relativepath = "\\temp\\oki.xml"; file f2 = new file(relativepath); system.out.println("does file exist using relative path? -> "+f2.exists()); runsax(relativepath); // using "wrong" relative path: string wrongrelativepath = "\\\\temp\\oki.xml"; file f3 = new file(wrongrelativepath); system.out.println("file relative path: "+f3.getpath()+" , file absolut path: "+f3.getabsolutepath()); runsax(wrongrelativepath); } private void runsax(string path) { seek { xmlreader parser = xmlreaderfactory.createxmlreader(); parser.parse(path); system.out.println(path + " well-formed."); } grab (saxexception e) { system.out.println(path + " not well-formed."); } grab (ioexception e) { system.out .println("due ioexception, parser not check " + path); } } private string getabsolutepath() { java.security.protectiondomain pd = saxsample.class .getprotectiondomain(); if (pd == null) homecoming null; java.security.codesource cs = pd.getcodesource(); if (cs == null) homecoming null; java.net.url url = cs.getlocation(); if (url == null) homecoming null; java.io.file f = new file(url.getfile()); if (f == null) homecoming null; homecoming f.getabsolutepath(); } }
which in case leads output (sorry dont know how format console output):
this class located under: d:\devjba\jba\bin file exist using absolut path? -> true d:\temp\oki.xml well-formed. file exist using relative path? -> true \temp\oki.xml well-formed. file relative path: \\temp\oki.xml , file absolut path: \\temp\oki.xml due ioexception, parser not check \\temp\oki.xml
since implied in comments wish able select files or directorys recomend have @ jfilechooser. simple way allow user take exists , provide absolute path selected file/files.
note: have no clue why in relative path-case right drive d:/ used , not other c:/
java string file-io ioexception
Comments
Post a Comment