fmg224
package com.fmg.reflect;

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

public class AbsolutePath {
    public static void main(String[] args) {
        // 线程获取绝对路径
        String path = Thread.currentThread().getContextClassLoader().getResource("Properties.Properties").getPath();
        System.out.println(path);
        // 线程加载对象
        InputStream io = Thread.currentThread().getContextClassLoader().getResourceAsStream("Properties.Properties");
        Properties pro = new Properties();
        try {
            pro.load(io);
            io.close();
            System.out.println(pro.get("username"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

获取路径的方法适用于 存在 src 项目类路径下的文件

 

分类:

技术点:

相关文章:

  • 2021-12-09
  • 2021-06-18
  • 2022-12-23
  • 2022-12-23
  • 2021-08-29
  • 2021-09-25
猜你喜欢
  • 2021-11-27
  • 2021-05-26
  • 2022-12-23
  • 2021-11-27
  • 2022-12-23
  • 2023-03-17
相关资源
相似解决方案