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
# java.library.path
# java.io.tmpdir
# java.compiler
# java.ext.dirs
# os.name
# os.arch
# os.version
# file.separator
# path.separator
# line.separator
# user.name
# user.home
# user.dir
1166

被折叠的 条评论
为什么被折叠?



