java往properties檔案中寫配置資訊

2021-08-30 17:12:07 字數 1290 閱讀 9631

上一節我們學習了讀取properties檔案中的資訊這一節我們來往配置檔案中寫資訊

程式**如下

system.out.println("進來了");

string address = request.getparameter("address");

string port = request.getparameter("port");

string username = request.getparameter("username");

string pass = request.getparameter("pass");

string dbname = request.getparameter("dbname");

string url = "jdbc:mysql://"+address+":"+port+"/"+dbname;

string path = request.getrealpath("");

//  資料庫配置檔案路徑,這裡要千萬注意不要寫相對路徑,剛開始我就寫的是src對應的路徑,後來經過多次嘗試終於找到原因了,必須寫web-inf 下classes檔案下的路徑這樣就是物理路徑就正確了

string conpath1 = path+"

\\web-inf\\classes\\com\\rhcy\\config\\db.properties

";string dburl = path+"

\\web-inf\\classes\\com\\rhcy\\config\\lwz.sql";

file file = new file(conpath1);

try catch (ioexception e)

properties properties = new properties();

try catch (ioexception e)

outputstream fos = new fileoutputstream(conpath1);

properties.setproperty("driver","com.mysql.jdbc.driver");  

properties.setproperty("url",url);  

properties.setproperty("username",username);  

properties.setproperty("password",pass);  

try catch (ioexception e)

} catch (filenotfoundexception e)          

java 載入properties 檔案

public static string mysql url public static string mysql uname public static string mysql passwd static catch ioexception e mysql url properties.getp...

java 讀取properties檔案

url 在實際開發工作中,我們又是會將一些路徑檔案配置放在properties檔案中,這樣我們需要修改路徑的時候就只需要寫該一下配置檔案就行了,不需要在 中挨個挨個的去改。但是我們怎樣獲得配置檔案中的值呢?其實這個很簡單,我們只需要封裝如下乙個工具類就行了 public class urlutil ...

java讀取properties檔案

學習心得 待完善。伺服器resin mysql druid資料庫連線池的配置檔案druid config.properties放在web inf classes目錄下 cmd視窗下直接執行編譯好的class檔案,能正確讀取properties檔案。但是,如果使用servlet呼叫類,就無法正確讀取。...