Java获取文件的绝对路径, Java 加载配置文件

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 项目类路径下的文件

 

posted @ 2021-06-22 21:30  深海里的星星i  阅读(301)  评论(0)    收藏  举报