`
壮志_凌云
  • 浏览: 33310 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
文章分类
社区版块
存档分类
最新评论

java操作Properties文件

阅读更多

原来的程序把连接数据库的代码都写在了JAVA代码里,这样在别的服务器里部署在tomcat后,想修改USER PASSWORD URL等信息非常麻烦,还是应该把这些属性写在Properties文件中比较方便。

package util;   
  
import java.io.InputStream;   
import java.util.Enumeration;   
import java.util.Properties;   
  
public class PropertiesTest {   
  
    public static void readProperties(String fileName) {   
        try {   
            Properties props = new Properties();   
  
            /*  
             String fileName1 = "src/util/oracle.properties";  
             BufferedInputStream bis = new BufferedInputStream(  
             new FileInputStream(fileName));*/  
  
            // String fileName = "oracle.properties";   
            InputStream in = PropertiesTest.class.getResourceAsStream(fileName);   
  
            props.load(in);   
            Enumeration enu = props.propertyNames();   
            while (enu.hasMoreElements()) {   
                String key = (String) enu.nextElement();   
                String property = props.getProperty(key);   
                System.out.println(key + "=" + property);   
            }   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
  
    public static void main(String[] args) {   
  
        String fileName = "db.properties";   
        readProperties(fileName);   
  
    }   
}  

 db.properties

driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
sysUrl=jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Monitor_Sys
dataUrl=jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Monitor_Data
user=sa  
password=123

 

0
0
分享到:
评论
2 楼 壮志_凌云 2010-03-04  
smiky 写道
你有想过PropertiesTest.class.getClassLoader().getResourceAsStream()与
PropertiesTest.class.getResourceAsStream()的区别吗?


不好意思啊,这个地方我以前就没怎么用过,刚接触,还没有仔细研究过它们的区别
1 楼 smiky 2010-03-01  
你有想过PropertiesTest.class.getClassLoader().getResourceAsStream()与
PropertiesTest.class.getResourceAsStream()的区别吗?

相关推荐

Global site tag (gtag.js) - Google Analytics