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 项目类路径下的文件
原文:https://www.cnblogs.com/l24118028/p/14920374.html