2012-08-24 64 views
3
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中引發了異常。

回答

5

在Netbeans中,您需要將該文件放在項目文件夾內而不是在src/package文件夾中。

0

您應該指定文件的完整路徑或將文件放到項目目錄中。項目目錄是運行項目時的當前目錄。

0

確保您的DBDriverInfo.properties位於CLASSPATH上。根據你的代碼,把你的屬性文件放到netbeans的默認包中。

0

File類的默認目錄是您開始執行主類的位置。在這些IDE的情況下,默認目錄將成爲您的項目主目錄。

爲了更好地瞭解您的默認目錄,請從IDE執行這兩行代碼。然後把你的文件放在那裏。

File f = new File("DBDriverInfo.properties"); 
System.out.println(f.getAbsolutePath()); 
相關問題