Java文件路径

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/abel004/article/details/82462294

system.getProperties()文档中列出了各种属性。

getProperties
public static Properties getProperties()Determines the current system properties.
First, if there is a security manager, its checkPropertiesAccess method is called with no arguments. This may result in a security exception.


The current set of system properties for use by the getProperty(String) method is returned as a Properties object. If there is no current set of system properties, a set of system properties is first created and initialized. This set of system properties always includes values for the following keys: Key Description of Associated Value


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

1.user.dir

 // 用户根目录,随运行环境变化。E:\javaStudy\imooc\001
 // linux:[root@localhost tmp]# java com/abel/Hello
 // /root/test/tmp
 // 在web中:D:\Program1024\tomcat8053\bin or /usr/local/tomcat8053/bin
 System.out.println(System.getProperty("user.dir"));

2.类加载的根路径

// /root/test/tmp or /usr/local/tomcat8053/webapps/aa/WEB-INF/classes
File f = new File(Hello.class.getResource("/").getPath());
System.out.println(f);

3.获取当前类的加载目录,不加”/”

// /root/test/tmp/com/abel or /usr/local/tomcat8053/webapps/aa/WEB-INF/classes/com/imooc
File f = new File(Hello.class.getResource("").getPath());
System.out.println(f);

4.获取项目路径

 // /root/test/tmp or /usr/local/tomcat8053/bin
 File directory = new File("");


 String courseFile = directory.getCanonicalPath();
 System.out.println(courseFile);

 // File.getCanonicalPath()和File.getAbsolutePath()大约只是对于new File(".")和new File("..")两种路径有所区别。 
 对于getCanonicalPath()函数,“."就表示当前的文件夹,而”..“则表示当前文件夹的上一级文件夹 
 对于getAbsolutePath()函数,则不管”.”、“..”,返回当前的路径加上你在new File()时设定的路径 
 至于getPath()函数,得到的只是你在new File()时设定的路径 

 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();                    //得到的是.. 

5.获取项目路径

// file:/root/test/tmp/
URL xmlpath = Hello.class.getClassLoader().getResource("");
System.out.println(xmlpath);

6.在Servlet中取得路径:

(1)得到工程目录:request.getSession().getServletContext().getRealPath(“”) 参数可具体到包名。
结果:E:/Tomcat/webapps/TEST
(2)得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/TEST/test
(3)得到相对地址:request.getRequestURI()
结果:/TEST/test

猜你喜欢

转载自blog.csdn.net/abel004/article/details/82462294