gpt4 book ai didi

java - 包中文件的绝对路径

转载 作者:太空宇宙 更新时间:2023-11-04 15:06:25 26 4
gpt4 key购买 nike

假设我有一个包:com.example.resources,里面有picture.jpgtext.txt - 如何我可以只使用包路径来识别picture.jpgtext.txt的绝对文件路径吗?

这是正确的吗?

File file1 = new File("/com/example/resources/picture.jpg");
file1.getAbsolutePath();

File file2 = new File("/com/example/resources/text.txt");
file2.getAbsolutePath();

最佳答案

"I'm trying to reference those files within the application so that when I deploy the application to different systems I won't have to reconfigure the location of each resource "

您不想使用将从本地计算机的文件系统加载的文件,您希望通过类路径加载,就像embedded resources那样。 应该阅读。使用getClass().getResourceAsStream(..)对于文本文件和 getClass().getReource(..)对于图像

鉴于您提供的包名称,您引用的当前路径看起来应该可以工作。

看看this answerthis answer作为一些引用。

关于java - 包中文件的绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21932263/

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