java获取默认用户目录_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()中的字符串参数如下:

Properties props=System.getProperties(); //系统属性

System.out.println("Java的运行环境版本:"+props.getProperty("java.version"));

System.out.println("Java的运行环境供应商:"+props.getProperty("java.vendor"));

System.out.println("Java供应商的URL:"+props.getProperty("java.vendor.url"));

System.out.println("Java的安装路径:"+props.getProperty("java.home"));

System.out.println("Java的虚拟机规范版本:"+props.getProperty("java.vm.specification.version"));

System.out.println("Java的虚拟机规范供应商:"+props.getProperty("java.vm.specification.vendor"));

System.out.println("Java的虚拟机规范名称:"+props.getProperty("java.vm.specification.name"));

System.out.println("Java的虚拟机实现版本:"+props.getProperty("java.vm.version"));

System.out.println("Java的虚拟机实现供应商:"+props.getProperty("java.vm.vendor"));

System.out.println("Java的虚拟机实现名称:"+props.getProperty("java.vm.name"));

System.out.println("Java运行时环境规范版本:"+props.getProperty("java.specification.version"));

System.out.println("Java运行时环境规范供应商:"+props.getProperty("java.specification.vender"));

System.out.println("Java运行时环境规范名称:"+props.getProperty("java.specification.name"));

System.out.println("Java的类格式版本号:"+props.getProperty("java.class.version"));

System.out.println("Java的类路径:"+props.getProperty("java.class.path"));

System.out.println("加载库时搜索的路径列表:"+props.getProperty("java.library.path"));

System.out.println("默认的临时文件路径:"+props.getProperty("java.io.tmpdir"));

System.out.println("一个或多个扩展目录的路径:"+props.getProperty("java.ext.dirs"));

System.out.println("操作系统的名称:"+props.getProperty("os.name"));

System.out.println("操作系统的构架:"+props.getProperty("os.arch"));

System.out.println("操作系统的版本:"+props.getProperty("os.version"));

System.out.println("文件分隔符:"+props.getProperty("file.separator")); //在 unix 系统中是"/"

System.out.println("路径分隔符:"+props.getProperty("path.separator")); //在 unix 系统中是":"

System.out.println("行分隔符:"+props.getProperty("line.separator")); //在 unix 系统中是"/n"

System.out.println("用户的账户名称:"+props.getProperty("user.name"));

System.out.println("用户的主目录:"+props.getProperty("user.home"));

System.out.println("用户的当前工作目录:"+props.getProperty("user.dir"));

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用Java中的File类来获取当前路径的绝对路径。具体代码如下: ``` File file = new File(""); String absolutePath = file.getAbsolutePath(); System.out.println("当前路径的绝对路径是:" + absolutePath); ``` 执行该代码后,控制台会输出当前路径的绝对路径。 ### 回答2: 在Java中,我们可以使用`System.getProperty("user.dir")`来获取当前工作目录的绝对路径。 `System.getProperty()`方法用于获取系统属性,默认情况下,它返回键值对的列表。其中,`"user.dir"`是一个系统属性,它表示用户当前工作目录。 例如,假设我们的Java程序位于路径`/Users/username/Documents/JavaProgram`中,并且我们想要获取当前工作目录的绝对路径。 我们可以使用以下代码来获取当前工作目录的绝对路径: ```java String currentDir = System.getProperty("user.dir"); System.out.println("当前工作目录的绝对路径为:" + currentDir); ``` 运行该程序,我们将会得到输出:`当前工作目录的绝对路径为:/Users/username/Documents/JavaProgram`。 通过调用`System.getProperty("user.dir")`方法,我们可以获取当前路径的绝对路径当前路径指的是程序运行时所在的目录。 ### 回答3: 在Java获取当前路径的绝对路径有多种方法。我将介绍两种常用的方法: 方法一: 可以使用`System.getProperty("user.dir")`方法来获取当前路径的绝对路径。该方法返回一个字符串,表示当前工作目录路径。示例如下: ```java String currentPath = System.getProperty("user.dir"); System.out.println("当前路径的绝对路径是:" + currentPath); ``` 方法二: 还可以使用`File`类来获取当前路径的绝对路径。首先需要创建一个`File`对象,传入一个文件或目录的相对路径作为参数。然后使用`getAbsolutePath()`方法获取该文件或目录的绝对路径。示例如下: ```java File file = new File("."); String currentPath = file.getAbsolutePath(); System.out.println("当前路径的绝对路径是:" + currentPath); ``` 这两种方法都能够获取当前路径的绝对路径,具体使用哪种方法取决于你的需求和编程风格。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值