gpt4 book ai didi

java - 使用Path类获取java中文件的资源路径

转载 作者:行者123 更新时间:2023-12-02 04:46:07 24 4
gpt4 key购买 nike

我是 Java 新手,请让我知道如何获取文件的资源路径而不是使用整个路径。下面是我的代码。

Path file = Paths.get("/home/Documents/src/test/resources/files/sample.txt");

我需要像 getResource("files/sample.txt") 这样的东西。

最佳答案

你需要这个:

URI uri = getClass().getClassLoader().getResource("/files/sample.txt").toURI(); 
Path path = Paths.get(uri);

关于java - 使用Path类获取java中文件的资源路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29650740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com