java获取路径

//获取当前类生成目录
System.out.println(Main.class.getResource("").getPath());
System.out.println(Main.class.getResource(".").getPath());
System.out.println(Main.class.getResource("./").getPath());

//获取当前类生成目录的上一级目录
System.out.println(Main.class.getResource("..").getFile());
System.out.println(Main.class.getResource("../").getPath());

//获取类生成根目录
System.out.println(Main.class.getResource("/").getPath());
System.out.println(Main.class.getClassLoader().getResource("").getPath());
System.out.println(Main.class.getClassLoader().getResource(".").getPath());
System.out.println(Main.class.getClassLoader().getResource("./").getPath());

System.out.println(ClassLoader.getSystemResources("").nextElement().getPath());
System.out.println(ClassLoader.getSystemResources(".").nextElement().getPath());
System.out.println(ClassLoader.getSystemResources("./").nextElement().getPath());

System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());
System.out.println(Thread.currentThread().getContextClassLoader().getResource(".").getPath());
System.out.println(Thread.currentThread().getContextClassLoader().getResource("./").getPath());

//获取项目根目录
File file = new File("");
System.out.println(file.getCanonicalPath());
System.out.println(file.getAbsolutePath());
//获取系统根目录
File file1 = new File("/");
System.out.println(file1.getCanonicalPath());
System.out.println(file1.getAbsolutePath());

//获取项目根目录
System.out.println(System.getProperty("user.dir"));
//获取用户主目录
System.out.println(System.getProperty("user.home"));
//获取java主目录
System.out.println(System.getProperty("java.home"));
//获取加载类时搜索的路径列表
System.out.println(System.getProperty("java.class.path"));
//获取加载库时搜索的路径列表
System.out.println(System.getProperty("java.library.path"));
//获取默认的临时文件路径
System.out.println(System.getProperty("java.io.tmpdir"));
//获取java扩展目录
System.out.println(System.getProperty("java.ext.dirs"));

输出结果:

/C:/develop/repository/test/out/production/test/me/jeff/
/C:/develop/repository/test/out/production/test/me/jeff/
/C:/develop/repository/test/out/production/test/me/jeff/
/C:/develop/repository/test/out/production/test/me/
/C:/develop/repository/test/out/production/test/me/
/C:/develop/repository/test/out/production/test/
/C:/develop/repository/test/out/production/test/
/C:/develop/repository/test/out/production/test/
/C:/develop/repository/test/out/production/test/
/C:/develop/repository/test/out/production/test/
/C:/develop/repository/test/out/production/test/
/C:/develop/repository/test/out/production/test/
/C:/develop/repository/test/out/production/test/
/C:/develop/repository/test/out/production/test/
/C:/develop/repository/test/out/production/test/
C:\develop\repository\test
C:\develop\repository\test
C:\
C:\
C:\develop\repository\test
C:\Users\Jeff Chou
C:\develop\Java\jdk1.8.0_121\jre
C:\develop\Java\jdk1.8.0_121\jre\lib\charsets.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\deploy.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\javaws.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\jce.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\jfr.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\jsse.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\management-agent.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\plugin.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\resources.jar;C:\develop\Java\jdk1.8.0_121\jre\lib\rt.jar;C:\develop\repository\test\out\production\test;C:\develop\IntelliJ IDEA\lib\idea_rt.jar
C:\develop\Java\jdk1.8.0_121\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\Oracle\Java\javapath;C:\develop\Java\jdk1.8.0_121\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\develop\Git\cmd;C:\develop\IntelliJ IDEA\plugins\maven\lib\maven3\bin;.
C:\Users\JEFFCH~1\AppData\Local\Temp\
C:\develop\Java\jdk1.8.0_121\jre\lib\ext;C:\Windows\Sun\Java\lib\ext
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值