给你一个关于Properties读与写的实例,希望对你有用。这个类已经测试通过。 注:要在你的classpath下新建一个文件:fda_init.properties,才能正常运行import java.io.*; import java.util.*;public class PropertiesOperate {
public int init_flag = 0; public int init_step = 0; public String driver = ""; public String db_ip = ""; public String db_name = ""; public String user = ""; public String password = "";
public static void main(String[] args) { PropertiesOperate po;
po = new PropertiesOperate(); po.readDBParam(); po.db_name = "scc"; po.saveDBParam();
po = new PropertiesOperate(); po.readDBParam(); System.out.println(po.db_name); po.db_name = "test"; po.saveDBParam();
po = new PropertiesOperate(); po.readDBParam(); System.out.println(po.db_name); } public boolean readDBParam() { Properties initProps = new Properties(); InputStream in = null; try { in = getClass().getResourceAsStream("/fda_init.properties"); initProps.load(in); } catch (Exception e) { System.err.println("Error reading FDA properties " + "in FDAGlobals"); e.printStackTrace(); return false; } finally { try { if (in != null) { in.close(); } } catch (Exception e) {} }
system.database.username = system
system.database.password = test不知道文本文件里面的文件是不是这样设置的呢?
cfgFileName = System.getProperties().getProperty("CONFIG_FILE");
cfgFileName 是不是为null
另外,System.getProperties()是获取系统属性的。
System.getProperties().getProperty("java.home");
你可以看一下jdk的帮助。
除非你在运行的时候加个开关,
比如java -D CONFIG_FILE=%CONFIG_FILE% test你可以试试看。
File cfgFile = new File(cfgFileName);
FileInputStream in = new FileInputStream(cfgFile);
Properties p = new Properties();
p.load(in);
dsName = p.getProperty("system.datasource.name");
username = p.getProperty("system.database.username");
password = p.getProperty("system.database.password");
in.close();
大家方便的话,不妨帮忙调试一下。再列出正确的代码及配置,谢谢啊!
为了看着方便,你可以这样。
java -Dconfig_file=%CONFIG_FILE% test读取的时候cfgFileName = System.getProperty("config_file");
注:要在你的classpath下新建一个文件:fda_init.properties,才能正常运行import java.io.*;
import java.util.*;public class PropertiesOperate {
public int init_flag = 0;
public int init_step = 0;
public String driver = "";
public String db_ip = "";
public String db_name = "";
public String user = "";
public String password = "";
public static void main(String[] args) {
PropertiesOperate po;
po = new PropertiesOperate();
po.readDBParam();
po.db_name = "scc";
po.saveDBParam();
po = new PropertiesOperate();
po.readDBParam();
System.out.println(po.db_name);
po.db_name = "test";
po.saveDBParam();
po = new PropertiesOperate();
po.readDBParam();
System.out.println(po.db_name);
} public boolean readDBParam() {
Properties initProps = new Properties();
InputStream in = null;
try {
in = getClass().getResourceAsStream("/fda_init.properties");
initProps.load(in);
}
catch (Exception e) {
System.err.println("Error reading FDA properties "
+ "in FDAGlobals");
e.printStackTrace();
return false;
}
finally {
try {
if (in != null) { in.close(); }
} catch (Exception e) {}
}
String temp;
if (initProps != null) {
temp=initProps.getProperty("initFlag");
try{
init_flag = Integer.parseInt(temp);
} catch (Exception e1) {
init_flag = -1;
}
temp=initProps.getProperty("initStep");
try{
init_step = Integer.parseInt(temp);
} catch (Exception e1) {
init_step = -1;
}
driver = initProps.getProperty("dbDriver");
db_ip = initProps.getProperty("dbIP");
db_name = initProps.getProperty("dbName");
user = initProps.getProperty("dbUsername");
password = initProps.getProperty("dbPassword"); return true;
}
return false;
}
public boolean saveDBParam(){
Properties initProps = new Properties();
OutputStream out = null;
try{
initProps.setProperty("initFlag",""+getInit_flag());
initProps.setProperty("initStep",""+getInit_step());
initProps.setProperty("dbDriver", getDriver());
initProps.setProperty("dbIP", getDb_ip());
initProps.setProperty("dbName", getDb_name());
initProps.setProperty("dbUsername", getUser());
initProps.setProperty("dbPassword", getPassword());
String fileName=getClass().getResource("../fda_init.properties").getFile();
out=new FileOutputStream(fileName);
initProps.store(out,"");
out.flush();
out.close() ;
return true;
}catch(Exception e){
e.printStackTrace() ;
System.out.println("Some exception happened!");
try{
out.close();
}catch(Exception er){}
return false;
}
}
/**
* @return
*/
public String getDb_ip() {
if(db_ip==null) return "";
else return db_ip;
} /**
* @return
*/
public String getDb_name() {
if(db_name==null) return "";
else return db_name;
}
/**
* @return
*/
public String getDriver() {
if(driver==null) return "";
else return driver;
} /**
* @return
*/
public int getInit_flag() {
return init_flag;
} /**
* @return
*/
public int getInit_step() {
return init_step;
} /**
* @return
*/
public String getPassword() {
if(password==null) return "";
else return password;
} /**
* @return
*/
public String getUser() {
if(user==null) return "";
else return user;
} /**
* @param string
*/
public void setDb_ip(String string) {
db_ip = string;
} /**
* @param string
*/
public void setDb_name(String string) {
db_name = string;
} /**
* @param string
*/
public void setDriver(String string) {
driver = string;
} /**
* @param string
*/
public void setInit_flag(int flag) {
init_flag = flag;
} /**
* @param string
*/
public void setInit_step(int step) {
init_step = step;
} /**
* @param string
*/
public void setPaswword(String string) {
password = string;
} /**
* @param string
*/
public void setUser(String string) {
user = string;
}}