获取项目路径/读配置文件演示

package com.apach.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

@SpringBootTest
class DemoApplicationTests {

    @Test
    void contextLoads() {
    }

    public static void main(String[] args) throws IOException {
        String path = DemoApplicationTests.class.getResource("").getPath();
        String path1 = DemoApplicationTests.class.getResource("/").getPath();
        String path2 = new File("/").getCanonicalPath();
        String path7 = new File("").getCanonicalPath();
        String path3 = DemoApplicationTests.class.getClassLoader().getResource("application.properties").getPath();
        String path4 = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        String path5 = System.getProperty("user.dir");
        String path6 = System.getProperty("java.class.path");

        Properties pro = new Properties();
        pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties"));
        String name = pro.getProperty("user.name");

        System.out.println("path = " + path);   //path = /D:/work/demo/target/test-classes/com/apach/demo/
        System.out.println("path1 = " + path1); //path1 = /D:/work/demo/target/test-classes/
        System.out.println("path2 = " + path2); //path2 = D:\
        System.out.println("path7 = " + path7); //path7 = D:\
        System.out.println("path3 = " + path3); //path3 = /D:/work/demo/target/classes/application.properties
        System.out.println("path4 = " + path4); //path4 = /D:/work/demo/target/test-classes/
        System.out.println("path5 = " + path5); //path5 = D:\work\demo
        System.out.println("path6 = " + path6); //path6 = ...
        System.out.println("name = " + name);   //name = aobama
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值