Java Get project Path

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.