java获取当前路径
来源:互联网 发布:淘宝店铺怎么自己装修 编辑:程序博客网 时间:2024/06/05 03:44
1、利用System.getProperty()函数获取当前路径:
System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径
2、使用File提供的函数获取当前路径:
File directory = new File("");//设定为当前文件夹
try{
}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();
direcotry.getPath();
File directory = new File(".");
directory.getCanonicalPath(); //得到的是C:\test
directory.getAbsolutePath();
direcotry.getPath();
File directory = new File("..");
directory.getCanonicalPath(); //得到的是C:\
directory.getAbsolutePath();
direcotry.getPath();
另外:System.getProperty()中的字符串参数如下:
System.getProperty()参数大全
# java.version
# java.vendor
# java.vendor.url
# java.home
# java.vm.specification.version
# java.vm.specification.vendor
# java.vm.specification.name
# java.vm.version
# java.vm.vendor
# java.vm.name
# java.specification.version
# java.specification.vendor
# java.specification.name
# java.class.version
# java.class.path