NetBeans 中的 Java 相对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7036513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:24:19  来源:igfitidea点击:

Java relative path in NetBeans

javanetbeansiofilereader

提问by Keshan

I am developing a NetBeans module where I have a Java package called testand another package called test.templates. I want to read a text file which is in the test.templatespackage from a Java file in the test package. I tried in several ways, but it gives a FileNotFoundExceptionexception:

我正在开发一个 NetBeans 模块,其中有一个名为 Java 的包test和另一个名为test.templates. 我想test.templates从测试包中的 Java 文件中读取包中的文本文件。我尝试了几种方法,但它给出了一个FileNotFoundException例外:

BufferedReader br = new BufferedReader(new FileReader("templates/test.txt"));
BufferedReader br = new BufferedReader(new FileReader("/test/templates/test.txt"));
BufferedReader br = new BufferedReader(new FileReader("src/test/templates/test.txt"));

But none of these worked.. I want to use the relative path, not the absolute path. What should I do?

但这些都不起作用..我想使用相对路径,而不是绝对路径。我该怎么办?

回答by Ray Toal

You will want to use getResourceor getResourceAsStream.

您将要使用getResourcegetResourceAsStream

Example on java2s.com:

java2s.com 上的示例:

http://www.java2s.com/Code/Java/Development-Class/Loadresourcefilerelativetotheclasslocation.htm

http://www.java2s.com/Code/Java/Development-Class/Loadresourcefilerelativetotheclasslocation.htm

回答by V? H?ng K?

You should note somethings about relative path (Netbeans):

您应该注意有关相对路径(Netbeans)的一些内容:

+ File: Default is project folder, means outside of srcfolder.
If you save to test.txt, it will generate: project/test.txt.
If you save to data/test.txt, ... project/data/test.txt
So if you want to load file, you just do it conversely. Like this, you should put your files in project/data/filename.txt. Then when code, you get path: data/filename.txt.

+ 文件:默认为项目文件夹,表示src文件夹外。
如果保存到test.txt,它将生成:project/test.txt
如果你保存到data/test.txt, ...project/data/test.txt
所以如果你想加载文件,你只需反过来做。像这样,你应该把你的文件放在 project/data/filename.txt 中。然后在编写代码时,您会得到 path: data/filename.txt

+ ImageIcon: I will share later if can.
+ Image(SplashScreen): I will share later.

+ ImageIcon:如果可以,我稍后会分享。
+ 图片(SplashScreen):我稍后会分享。

回答by Megatron

getResource()returns a URL, so to extract the filename, you can try calling getFile().

getResource()返回一个 URL,因此要提取文件名,您可以尝试调用getFile().

The filepath you pass to getResource will be based on your netbeans package. Use a leading slash to denote the root of the classpath.

您传递给 getResource 的文件路径将基于您的 netbeans 包。使用前导斜杠表示类路径的根。

Example:

例子:

getResource(/db_files/table.csv).getFile()

回答by Joyal Augustine

try
{
BufferedReader br = new BufferedReader(new FileReader(getClass().getResource("/test/templates/test.txt").toString().substring(6)));
}
catch(Exception ee)
{
JOptionPane.showMessageDialog(this, ee);   
}