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
文件位於相同的目錄中,但會引發以下異常。在命令行界面使用javac
編譯時如何使用NetBeans中的相對路徑加載文件
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)
相對路徑工作正常。 但NetBeans中引發了異常。