자바 가 프로그램 이나 프로젝트 경 로 를 가 져 오 는 데 자주 사용 되 는 방법

1313 단어 자바
자바 프로그램 을 쓸 때 파일 을 가 져 오 는 경 로 를 피 할 수 없습니다. 자주 사용 하 는 방법 은 다음 과 같 습 니 다.
1. 임의의 class 에서 호출:
this.getClass().getClassLoader().getResource("/").getPath();

//      classes      

//  : E:\eclipseM9/workspace/tree/WEB-INF/classes/

이 방법 도 웹 환경 에서 경 로 를 확정 하지 않 아 도 비교적 쓰기 좋다.
2 servlet 의 init 방법 에서
String path = getServletContext().getRealPath("/");

//    web      

//   :E:\eclipseM9\workspace\tree\

//tree  web      

3. 웹 루트 의 컨 텍스트 환경 가 져 오기
request.getContextPath();

//  /tree    web   root context

/*jsp          */

path=request.getRealPath("");

/*  jbossWEB       warUrl=.../tmp/deploy/tmp14544test-exp.war/*/

path=C:\jboss-4.0.5.GA\server\default\tmp\deploy\tmp14544test-exp.war\

String   path   =   (String)request.getContextPath();

/*    (test)           path=/test*/ 

String     path     =   request.getRequestURI();

/*             path=/test/admin/admindex.jsp*/

String   savePath=request.getRealPath(request.getServletPath());

/*             */  

//JAVA          

File   file=new   File(".");   

String path=file.getAbsolutePath();

                path=file.getPath();

/*  jboss     path=C:\jboss-4.0.5.GA\bin\*/

 

좋은 웹페이지 즐겨찾기