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/