[英]How to load files using relative path in NetBeans
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文件夾中。
您應該指定文件的完整路徑或將文件放到項目目錄中。 項目目錄是運行項目時的當前目錄。
確保您的DBDriverInfo.properties位於CLASSPATH上。 根據您的代碼,將您的屬性文件放在netbeans的默認包中。
File類的默認目錄是您開始執行主類的默認目錄。 對於這些IDE,默認目錄將是您的項目主目錄。
要更好地了解您的默認目錄,請從IDE執行這兩行。 然后將文件放在那里。
File f = new File("DBDriverInfo.properties");
System.out.println(f.getAbsolutePath());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.