java - 如何在 NetBeans 中使用相对路径加载文件

标签 java netbeans properties netbeans-7 fileinputstream

import java.io.*;
import java.util.Properties;

public class NewClass {
    public static void main(String args[]) throws IOException  {
        Properties p = new Properties();
        p.load(new FileInputStream("DBDriverInfo.properties"));
        String url=p.getProperty("url");
        String user=p.getProperty("username");
        String pass=p.getProperty("password");
        System.out.println(url+"\n"+user+"\n"+pass);
    }
}

虽然文件 DBDriverInfo.properties 文件在同一目录中,但引发了以下异常。

Exception in thread "main" java.io.FileNotFoundException: DBDriverInfo.properties (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:97)
    at NewClass.main(NewClass.java:7)

在命令行界面中使用 javac 编译时,相对路径工作正常。 但在 NetBeans 中会出现异常。

最佳答案

在 Netbeans 中,您需要将该文件放在项目文件夹中,而不是放在 src/package 文件夹中。

关于java - 如何在 NetBeans 中使用相对路径加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12107334/

相关文章:

java - Arquillian 与 Glassfish,resourcesXml 属性似乎不是有效的文件路径

ios - 属性(property)还是非属性(property)?

java - 获取已编译类的源代码

maven - 导入 Material list 中使用的 maven 属性(bom)

c# - 检查两个对象实例以查看它们是否相同

java - Android Maven 无法解析配置的依赖关系

Java switch case - 默认与显式枚举

java - hadoop中基于内容的推荐器

java - 套接字编程、Java、Tomcat 6、扩展

java - Netbeans 警告 pom.xml 中 "http://download.java.net/maven/2"的黑名单存储库声明。如何解决?