Java中獲取當(dāng)前路徑的幾種方法總結(jié)
1、利用System.getProperty()函數(shù)獲取當(dāng)前路徑:
1
|
System.out.println(System.getProperty( "user.dir" )); //user.dir指定了當(dāng)前的路徑 |
2、使用File提供的函數(shù)獲取當(dāng)前路徑:
1
2
3
4
5
|
File directory = new File( "" ); //設(shè)定為當(dāng)前文件夾 try { System.out.println(directory.getCanonicalPath()); //獲取標(biāo)準(zhǔn)的路徑 System.out.println(directory.getAbsolutePath()); //獲取絕對(duì)路徑 } catch (Exceptin e){} |
File.getCanonicalPath()和File.getAbsolutePath()大約只是對(duì)于new File(".")和new File("..")兩種路徑有所區(qū)別。
# 對(duì)于getCanonicalPath()函數(shù),“."就表示當(dāng)前的文件夾,而”..“則表示當(dāng)前文件夾的上一級(jí)文件夾
# 對(duì)于getAbsolutePath()函數(shù),則不管”.”、“..”,返回當(dāng)前的路徑加上你在new File()時(shí)設(shè)定的路徑
# 至于getPath()函數(shù),得到的只是你在new File()時(shí)設(shè)定的路徑
比如當(dāng)前的路徑為 C:/test :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
File directory = new File( "abc" ); directory.getCanonicalPath(); //得到的是C:/test/abc directory.getAbsolutePath(); //得到的是C:/test/abc direcotry.getPath(); //得到的是abc File directory = new File( "." ); directory.getCanonicalPath(); //得到的是C:/test directory.getAbsolutePath(); //得到的是C:/test/. direcotry.getPath(); //得到的是. File directory = new File( ".." ); directory.getCanonicalPath(); //得到的是C:/ directory.getAbsolutePath(); //得到的是C:/test/.. direcotry.getPath(); //得到的是.. |
另外:System.getProperty()中的字符串參數(shù)如下:
System.getProperty()參數(shù)大全
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# java.version Java Runtime Environment version # java.vendor Java Runtime Environment vendor # java.vendor.url Java vendor URL # java.home Java installation directory # java.vm.specification.version Java Virtual Machine specification version # java.vm.specification.vendor Java Virtual Machine specification vendor # java.vm.specification.name Java Virtual Machine specification name # java.vm.version Java Virtual Machine implementation version # java.vm.vendor Java Virtual Machine implementation vendor # java.vm.name Java Virtual Machine implementation name # java.specification.version Java Runtime Environment specification version # java.specification.vendor Java Runtime Environment specification vendor # java.specification.name Java Runtime Environment specification name # java. class .version Java class format version number # java. class .path Java class path # java.library.path List of paths to search when loading libraries # java.io.tmpdir Default temp file path # java.compiler Name of JIT compiler to use # java.ext.dirs Path of extension directory or directories # os.name Operating system name # os.arch Operating system architecture # os.version Operating system version # file.separator File separator ( "/" on UNIX) # path.separator Path separator ( ":" on UNIX) # line.separator Line separator ( "/n" on UNIX) # user.name User's account name # user.home User's home directory # user.dir User's current working directory |
JAVA中獲取路徑:
1.jsp中取得路徑:
以工程名為TEST為例:
(1)得到包含工程名的當(dāng)前頁(yè)面全路徑:
1
|
request.getRequestURI() |
結(jié)果:/TEST/test.jsp
(2)得到工程名:
1
|
request.getContextPath() |
結(jié)果:/TEST
(3)得到當(dāng)前頁(yè)面所在目錄下全名稱:
1
|
request.getServletPath() |
結(jié)果:如果頁(yè)面在jsp目錄下 /TEST/jsp/test.jsp
(4)得到頁(yè)面所在服務(wù)器的全路徑:
1
|
application.getRealPath( "頁(yè)面.jsp" ) |
結(jié)果:D:/resin/webapps/TEST/test.jsp
(5)得到頁(yè)面所在服務(wù)器的絕對(duì)路徑:
1
|
absPath= new java.io.File(application.getRealPath(request.getRequestURI())).getParent(); |
結(jié)果:D:/resin/webapps/TEST
2.在類中取得路徑:
(1)類的絕對(duì)路徑:
1
|
Class. class .getClass().getResource( "/" ).getPath() |
結(jié)果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路徑:
1
|
System.getProperty( "user.dir" ) |
結(jié)果:D:/TEST
3.在Servlet中取得路徑:
(1)得到工程目錄:
1
|
request.getSession().getServletContext().getRealPath( "" ) 參數(shù)可具體到包名。 |
結(jié)果:E:/Tomcat/webapps/TEST
(2)得到IE地址欄地址:
1
|
request.getRequestURL() |
結(jié)果:http://localhost:8080/TEST/test
(3)得到相對(duì)地址:
1
|
request.getRequestURI() |
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
原文鏈接:http://blog.csdn.net/qq_35101189/article/details/58059377