ImportJava.io.File;/*** New File (".. \path\abc.txt ") Three ways to get Paths <br> * 1:getpath () Get relative paths, for example: \path\abc.txt <br> * 2:getabslutlypath () Gets the absolute path, but may contain ".." or "." characters, such as D:\otherPath\: \path\abc.txt <br> * 3:getcanonicalpath () Gets the absolute path, but does not contain ".." or "." characters, such as D:\path\abc.txt*/ Public classPathutil {Private StaticString Webrootpath; Private StaticString Rootclasspath; @SuppressWarnings ("Rawtypes") Public Staticstring GetPath (Class clazz) {string path= Clazz.getresource (""). GetPath (); return NewFile (path). GetAbsolutePath (); } Public Staticstring GetPath (Object object) {string path= Object.getclass (). GetResource (""). GetPath (); return NewFile (path). GetAbsolutePath (); } Public StaticString Getrootclasspath () {if(Rootclasspath = =NULL) { Try{String path= Pathutil.class. getClassLoader (). GetResource (""). Touri (). GetPath (); Rootclasspath=NewFile (path). GetAbsolutePath (); } Catch(Exception e) {String path= Pathutil.class. getClassLoader (). GetResource (""). GetPath (); Rootclasspath=NewFile (path). GetAbsolutePath (); } } returnRootclasspath; } Public StaticString Getpackagepath (Object object) {package P=Object.getclass (). Getpackage (); returnP! =NULL? P.getname (). replaceall ("\ \", "/"): ""; } Public Staticfile Getfilefromjar (String file) {Throw NewRuntimeException ("Not finish. Don't use the This method.); } Public StaticString Getwebrootpath () {if(Webrootpath = =NULL) Webrootpath=Detectwebrootpath (); returnWebrootpath; } Public Static voidSetwebrootpath (String webrootpath) {if(Webrootpath = =NULL) return; if(Webrootpath.endswith (file.separator)) Webrootpath= webrootpath.substring (0, Webrootpath.length ()-1); Pathutil.webrootpath=Webrootpath; } Private StaticString Detectwebrootpath () {Try{String path= Pathutil.class. GetResource ("/"). Touri (). GetPath (); return NewFile (path). Getparentfile (). Getparentfile (). Getcanonicalpath (); } Catch(Exception e) {Throw NewRuntimeException (e); } }}
Java Get project Path