首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

怎么读取properties文件中属性

2013-11-15 
如何读取properties文件中属性?----------------------------------------------------------------------

如何读取properties文件中属性

?
-----------------------------------------------------------------------------------------
DataDAO.java

package com.zh.model;

import javax.sql.DataSource;

public class DataDAO {
private DataSource datasource;

public DataSource getDatasource() {
return datasource;
}

public void setDatasource(DataSource datasource) {
this.datasource = datasource;
}

}
------------------------------------------------------------------------------------
测试连接是否成功,test.java
package com.zh.logic;

import java.sql.Connection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.zh.model.DataDAO;

public class test {
public static void main(String [] args){
try{
String[] path = {"spring.xml"};
ApplicationContext ctx = new FileSystemXmlApplicationContext(path);

DataDAO dao = (DataDAO)ctx.getBean("dao");
Connection con = dao.getDatasource().getConnection();
System.out.println(con.isClosed());
//System.out.print(dao.getName());
}catch(Exception ex){
ex.printStackTrace();
}
}
}
-------------------------------------------------------------------------------------
2.用java.util.Properties这个类来读取
比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,如:
ip=192.168.0.1
port=8080
--------------------------------------------------------------------------------------
则,我们可以用如下程序来获得服务器配置信息:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("database.properties");
Properties p = new Properties();
try{
p.load(inputStream);
} catch (IOException e1){
e1.printStackTrace();
}
System.out.println("url:"+p.getProperty("jdbc.url")+"username"+p.getProperty("jdbc.username"));
--------------------------------------------------------------------------------------
上面介绍了读取properties的内容,现实中我们还有可能要修改文件的内容,下面就看下怎么修改properties的内容,文件还是上面那个:
package com.zh.logic;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

public class TestRead {

public void read(){
try {
InputStream in = this.getClass().getClassLoader().getResourceAsStream("config/host.properties");
Properties p = new Properties();
p.load(in);
//p.list(System.out);

System.out.println(p.getProperty("ip")+","+p.getProperty("username")+","+p.getProperty("pwd"));
} catch (Exception e) {
e.printStackTrace();
}
}

public void update(String path){
try{
Properties p = new Properties();
FileInputStream in = new FileInputStream(path);
p.load(in);
FileOutputStream out = new FileOutputStream(path);

p.setProperty("ip","1234567");
p.store(out,"ip update");
//p.save(out,"ip updated");
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args){
TestRead td = new TestRead();
td.read();
td.update("config/host.properties");
td.read();
}
}
可以看见修改之前的和修改之后的内容有改变;在上面有点要注意的:
FileInputStream in = new FileInputStream(path);
p.load(in);
FileOutputStream out = new FileOutputStream(path);
就是p.load(in);要写在FileOutputStream out = new FileOutputStream(path);之前,不然的话,修改后的文件内容就成了ip=1234567,而port=8080这句被覆盖了;

?

方法3:

package com.util;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;public class DBUtil {public static void main(String[] args){   DBUtil db = new DBUtil();   db.readProperties();    }public void readProperties(){   try {    Properties props = new Properties();    //第一种读取 properties 方法    props.load(getClass().getResourceAsStream("/config/oracleConn.properties"));           //-------------------------------------------------------------    //读取键值    String oracle_url = props.getProperty("oracle_url");    String oracle_name = props.getProperty("oracle_name");    String oracle_user = props.getProperty("oracle_user");    String oracle_pwd = props.getProperty("oracle_pwd");       Class.forName("oracle.jdbc.driver.OracleDriver"); //加载oracle驱动    Connection conn = DriverManager.getConnection(oracle_url,oracle_user,oracle_pwd);    System.out.println(conn);       conn.close();   } catch (FileNotFoundException e) {    e.printStackTrace();   } catch (IOException e) {    e.printStackTrace();   } catch (ClassNotFoundException e) {    e.printStackTrace();   } catch (SQLException e) {    e.printStackTrace();   }}}

?

?

?

?

引自:http://hi.baidu.com/alizv/item/358543e1b6ad2df32a09a426

热点排行