从网上的视频中看到可以通过以下方法获取一个文件的绝对路径,但当我将代码复制下来运行后,老是发现空指针异常。

从网上的视频中看到可以通过以下方法获取一个文件的绝对路径,但当我将代码复制下来运行后,老是发现空指针异常。

public class Test04 {
    public static void main(String[] args) {

                String path=Thread.currentThread().getContextClassLoader()
                        .getResource("path.properties").getPath();
                System.out.println(path);

    }
}

img

我尝试过将其放在src目录下,网上说那是根路径,结果一样无法得到绝对路径。

你把path.properties放到resources目录下吧