当前位置: 移动技术网 > IT编程>开发语言>Java > Java中获取当前路径的几种方法总结

Java中获取当前路径的几种方法总结

2019年07月22日  | 移动技术网IT编程  | 我要评论

java中获取当前路径的几种方法总结

1、利用system.getproperty()函数获取当前路径:

system.out.println(system.getproperty("user.dir"));//user.dir指定了当前的路径 

2、使用file提供的函数获取当前路径:

file directory = new file("");//设定为当前文件夹 
try{ 
  system.out.println(directory.getcanonicalpath());//获取标准的路径 
  system.out.println(directory.getabsolutepath());//获取绝对路径 
}catch(exceptin e){} 

file.getcanonicalpath()和file.getabsolutepath()大约只是对于new file(".")和new file("..")两种路径有所区别。

# 对于getcanonicalpath()函数,“."就表示当前的文件夹,而”..“则表示当前文件夹的上一级文件夹
# 对于getabsolutepath()函数,则不管”.”、“..”,返回当前的路径加上你在new file()时设定的路径
# 至于getpath()函数,得到的只是你在new file()时设定的路径

比如当前的路径为 c:/test :

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()中的字符串参数如下:

system.getproperty()参数大全

# 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)得到包含工程名的当前页面全路径:

request.getrequesturi() 

结果:/test/test.jsp

(2)得到工程名:

request.getcontextpath() 

结果:/test

(3)得到当前页面所在目录下全名称:

request.getservletpath() 

结果:如果页面在jsp目录下 /test/jsp/test.jsp

(4)得到页面所在服务器的全路径:

application.getrealpath("页面.jsp") 

结果:d:/resin/webapps/test/test.jsp

(5)得到页面所在服务器的绝对路径:

abspath=new java.io.file(application.getrealpath(request.getrequesturi())).getparent(); 

结果:d:/resin/webapps/test

2.在类中取得路径:

(1)类的绝对路径:

class.class.getclass().getresource("/").getpath() 

结果:/d:/test/webroot/web-inf/classes/pack/

(2)得到工程的路径:

system.getproperty("user.dir") 

结果:d:/test

3.在servlet中取得路径:

(1)得到工程目录:

request.getsession().getservletcontext().getrealpath("") 参数可具体到包名。 

结果:e:/tomcat/webapps/test

(2)得到ie地址栏地址:

request.getrequesturl() 

结果:http://localhost:8080/test/test

(3)得到相对地址:

request.getrequesturi() 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网