Java路径问题,包括web项目和空格问题

以下是常用的java路径获取方法:

        String userDir = System.getProperty("user.dir");
        System.out.println("user.dir       :" + userDir);
        String userJome = System.getProperty("user.home");
        System.out.println("user.home      :" + userJome);
        String javaClassPath = System.getProperty("java.class.path");
        System.out.println("java.class.path:" + javaClassPath);

        String canonicalPath = new File("").getCanonicalPath();
        System.out.println("canonicalPath  :" + canonicalPath);// 获取标准路径
        String absolutePath = new File("").getAbsolutePath();
        System.out.println("absolutePath   :" + absolutePath);// 获取绝对路径

        String currentClassPath = this.getClass().getResource("").toURI().getPath();
        System.out.println("currentClassPath:" + currentClassPath);// 获取当前类路径
        String LoadClassPath = this.getClass().getClassLoader().getResource("").toURI().getPath();
        System.out.println("LoadClassPath   :" + LoadClassPath);// 获取加载类路径


以下是java web项目路径获取方法(toURI解决了空格问题;由于编写完成代码之后我们要进行单元测试,所以projectPath要有两种获取方式):
        String projectPath;
        try {
            projectPath = this.getClass().getClassLoader().getResource("").toURI().getPath() + "users.xml";
        } catch (NullPointerException e) {
            projectPath = System.getProperty("user.dir") + "\\users.xml";
        }
        System.out.println("projectPath:" + projectPath);// 获取项目路径

以下是他人收集的非常好的网址:

Java路径问题解决方案收集:http://www.cnblogs.com/rongxh7/archive/2010/04/22/1718178.html
Java获取当前路径:http://www.cnblogs.com/diyunpeng/archive/2011/06/06/2073567.html
java(Web)中相对路径,绝对路径问题总结:http://www.blogjava.net/meil/archive/2006/10/10/73908.html


粗浅理解,还望同学们指正。

欢迎阅读相关文章: http://hi.csdn.net/linchengzhi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值