JAVA 获取路径问题

1. 项目根路径获取

(1)new File("").getAbsolutePath()

输出:=getAbsolutePath====D:\project\SVN\2-系统开发实施\(3)编码\personnel-positioning-data-collector

(2) System.getProperty("user.dir")

输出:====user.dir===D:\project\SVN\2-系统开发实施\(3)编码\personnel-positioning-data-collector

(3) new File("").getCanonicalPath();

输出:======file.getFCanonicalPath=====D:\project\SVN\2-系统开发实施\(3)编码\personnel-positioning-data-collector

2.类资源加载路径获取

System.out.println("===类资源根路径getResource(/)====" : GeoShapServiceImpl.class.getResource("/").getPath());

输出:===getResource(/)====/D:/project/SVN/2-%e7%b3%bb%e7%bb%9f%e5%bc%80%e5%8f%91%e5%ae%9e%e6%96%bd/(3)%e7%bc%96%e7%a0%81/personnel-positioning-data-collector/positioning-data-geomapfile/target/classes/

 System.out.println("====当前类资源路径getResource()===" + GeoShapServiceImpl.class.getResource("").getPath());

输出:====getResource()===/D:/project/SVN/2-%e7%b3%bb%e7%bb%9f%e5%bc%80%e5%8f%91%e5%ae%9e%e6%96%bd/(3)%e7%bc%96%e7%a0%81/personnel-positioning-data-collector/positioning-data-geomapfile/target/classes/geomapfile/service/impl/

3.获取类库资源加载路径

System.out.println("==========java.class.path======" + System.getProperty("java.class.path"));
输出(截取部分):==========java.class.path======C:\Program Files\Java\jdk1.8.0_241\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\jfxrt.jar;C:\Program Fpositioning-data-collector\positioning-data-geomapfile\target\classes;D:\Users\zhanglu\local\apache-maven-3.6.1\my-maven\org\springframework\boot\spring-boot-starter\2.0.4.RELEASE\spring-boot-starter-2.0.4.RELEASE.jar;D:\Users\zhanglu\local\apache-maven-3.6.1\my-maven\org\springframework\boot\spring-boot\2.0.4.RELEASE\spring-boot-2.0.4.RELEASE.jar;D:\Users\zhanglu\local\apache-maven-3.6.1\my-maven\org\springframework\spring-context\5.0.8.RELEASE\spring-context-5.0.8.RELEASE.jar;D:\Users\zhanglu\local\apache-maven-3.6.1\my-maven\org\springframework\boot\spring-boot-autoconfigure\2.0.4.RELEASE\spring-boot-autoconfigure-2.0.4.RELEASE.jar;D:\Users\zhanglu\local\apache-maven-3.6.1\my-maven\org\springframework\boot\spring-boot-starter-logging\2.0.4.RELEASE\spring-boot-starter-logging-2.0.4.RELEASE.jar;D:\Users\zhanglu\local\apache-maven-3.6.1\my-maven\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar

4.解决Properties中文乱码问题

其中需要把properties放到resources中,与application.yml在同级路径,结构如下图所示:

 注:强调下文件路径存放问题主要是解决InputStream 对象为null的情况

 try {
//        InputStream in = new FileInputStream(new File(Thread.currentThread().getContextClassLoader().getResource("shapfile.properties").getFile()));

            InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("shapfile.properties");
            BufferedReader bf = new BufferedReader(new InputStreamReader(in, "gbk"));
            Properties prop = new Properties();
            String value = "";

            prop.load(bf);
            value = prop.getProperty("CQ");
            System.out.println(value);
        } catch (IOException e) {
            e.printStackTrace();
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值