[英]Java Fileinputstream File not found
“ dbconnection.java”“ db.properties”文件找不到該文件。 我向您展示如何獲取以下文件。
我的下一個項目目錄。
src
D B
db.properties
DbConnection.java
public class DBConnection {
public static Connection getConnection() {
Properties props = new Properties();
FileInputStream fis = null;
Connection con = null;
try {
fis = new FileInputStream("db.properties");
props.load(fis);
out.println(props.getProperty("DB_DRIVER_CLASS"));
// load the Driver Class
Class.forName(props.getProperty("DB_DRIVER_CLASS"));
// create the connection now
con = DriverManager.getConnection(props.getProperty("DB_URL"),
props.getProperty("DB_USERNAME"),
props.getProperty("DB_PASSWORD"));
} catch (IOException | ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
}
fis = new FileInputStream(“ db.properties”);
該行在運行應用程序的“主”目錄下查找文件。 它等效於“ ./db.properties”。 如果您不知道應用程序的根目錄,請嘗試使用此行添加調試。new File(“。”)。getAbsolutePath(); 然后將您的屬性文件移到那里。
在上面的代碼中,請提供文件的完整路徑(db.properties)。 也就是說,例如,如果文件位於“ E”驅動器“ projectA”文件夾中,則路徑為
fis = new FileInputStream("E:/projectA/db.properties");// will work
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.