JAVA属性文件的操作类Propertise

系统 1273 0

 

J2SE API 读取 Properties 文件六种方法

http://webservices.ctocio.com.cn/115/8689615.shtml

 

1。使用 Java .util. Properties 类的load()方法

  示例:InputStreamin=lnewBufferedInputStream(newFileInputStream(name));

  Propertiesp=newProperties();

  p.load(in);

  2。使用 java .util.ResourceBundle类的getBundle()方法

  示例:ResourceBundlerb=ResourceBundle.getBundle(name,Locale.getDefault());

  3。使用 java .util.PropertyResourceBundle类的构造函数

  示例:InputStreamin=newBufferedInputStream(newFileInputStream(name));

  ResourceBundlerb=newPropertyResourceBundle(in);

  4。使用class变量的getResourceAsStream()方法

  示例:InputStreamin=JProperties.class.getResourceAsStream(name);

  Propertiesp=newProperties();

  p.load(in);

  5。使用class.getClassLoader()所得到的 java .lang.ClassLoader的getResourceAsStream()方法

  示例:InputStreamin=JProperties.class.getClassLoader().getResourceAsStream(name);

  Propertiesp=newProperties();

  p.load(in);

  6。使用 java .lang.ClassLoader类的getSystemResourceAsStream()静态方法

  示例:InputStreamin=ClassLoader.getSystemResourceAsStream(name);

  Propertiesp=newProperties();

  p.load(in);

  补充

  Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法

  示例:InputStreamin=context.getResourceAsStream(path);

  Propertiesp=newProperties();

  p.load(in);

 

这个类的作用在于帮你解决连接数据库时的 " 硬编码 " 问题 , 即驱动类 , 连接字符串 , 用户名 , 密码这些信息可以通过资源文件来获得 , 这种做法既增加了安全性 , 又使代码容易维护 .

  

处理数据库资源文件的类   DBConfig. java

 

这个类的作用在于帮你解决连接数据库时的 " 硬编码 " 问题 , 即驱动类 , 连接字符串 , 用户名 , 密码这些信息可以通过资源文件来获得 , 这种做法既增加了安全性 , 又使代码容易维护 .

  

处理数据库资源文件的类   DBConfig. java

 

Java 代码 复制代码
  1. import   java .util. Properties ;  
  2. import   java .io.*;  
  3.   
  4. public   class  DBConfig {  
  5.      private   static  Object initLock =  new  Object();  
  6.   
  7.      private   static  DBConfig dbconfig =  null ;  
  8.   
  9.      private   Properties  props =  null ;  
  10.   
  11.      public   static  DBConfig getInstance() {  
  12.          if  (dbconfig ==  null ) {  
  13.              synchronized  (initLock) {  
  14.                  if  (dbconfig ==  null ) {  
  15.                     dbconfig =  new  DBConfig();  
  16.                 }  
  17.             }  
  18.         }  
  19.          return  dbconfig;  
  20.     }  
  21.   
  22.      private   synchronized   void  loadProperties() {  
  23.         props =  new   Properties ();  
  24.          try  {  
  25.             System.out.println( "Load pro file" );  
  26.             InputStream in = getClass().getResourceAsStream( "/db. properties " );  
  27.             props.load(in);  
  28.         }  catch  (Exception e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.     }  
  32.   
  33.      public  String getProperty(String propName) {  
  34.          if  (props ==  null ) {  
  35.             loadProperties();  
  36.         }  
  37.          return  props.getProperty(propName);  
  38.     }  
  39. }  
Java 代码 复制代码
  1. import   java .util. Properties ;   
  2. import   java .io.*;   
  3.   
  4. public   class  DBConfig {   
  5.      private   static  Object initLock =  new  Object();   
  6.   
  7.      private   static  DBConfig dbconfig =  null ;   
  8.   
  9.      private   Properties  props =  null ;   
  10.   
  11.      public   static  DBConfig getInstance() {   
  12.          if  (dbconfig ==  null ) {   
  13.              synchronized  (initLock) {   
  14.                  if  (dbconfig ==  null ) {   
  15.                     dbconfig =  new  DBConfig();   
  16.                 }   
  17.             }   
  18.         }   
  19.          return  dbconfig;   
  20.     }   
  21.   
  22.      private   synchronized   void  loadProperties() {   
  23.         props =  new   Properties ();   
  24.          try  {   
  25.             System.out.println( "Load pro file" );   
  26.             InputStream in = getClass().getResourceAsStream( "/db. properties " );   
  27.             props.load(in);   
  28.         }  catch  (Exception e) {   
  29.             e.printStackTrace();   
  30.         }   
  31.     }   
  32.   
  33.      public  String getProperty(String propName) {   
  34.          if  (props ==  null ) {   
  35.             loadProperties();   
  36.         }   
  37.          return  props.getProperty(propName);   
  38.     }   
  39. }  
    import 
    
      
        java
      
    
    .util.
    
      
        Properties
      
    
    ;
import 
    
      
        java
      
    
    .io.*;

public class DBConfig {
	private static Object initLock = new Object();

	private static DBConfig dbconfig = null;

	private 
    
      
        Properties
      
    
     props = null;

	public static DBConfig getInstance() {
		if (dbconfig == null) {
			synchronized (initLock) {
				if (dbconfig == null) {
					dbconfig = new DBConfig();
				}
			}
		}
		return dbconfig;
	}

	private synchronized void loadProperties() {
		props = new 
    
      
        Properties
      
    
    ();
		try {
			System.out.println("Load pro file");
			InputStream in = getClass().getResourceAsStream("/db.
    
      
        properties
      
    
    ");
			props.load(in);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getProperty(String propName) {
		if (props == null) {
			loadProperties();
		}
		return props.getProperty(propName);
	}
}
  

 

 

资源文件  db. properties

url=jdbc:mysql://localhost:3306/example

driver=org.gjt.mm.mysql.Driver

username=root

password=123654

  在连接数据库的代码中 , 可以通过以下方式得到驱动类 ,url,username,password

 

Java 代码 复制代码
  1. String driver       =   DBConfig.getInstance().getProperty( "driver" );  
  2. String url          =   DBConfig.getInstance().getProperty( "url" );  
  3. String username =   DBConfig.getInstance().getProperty( "username" );  
  4. String password =   DBConfig.getInstance().getProperty( "password" );  
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="0" height="0" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"> <param name="src" value="http://peaklui.iteye.com/javascripts/syntaxhighlighter/clipboard.swf"> <embed type="application/x-shockwave-flash" width="0" height="0" src="http://peaklui.iteye.com/javascripts/syntaxhighlighter/clipboard.swf"></embed></object>
Java 代码 复制代码
  1. String driver       =   DBConfig.getInstance().getProperty( "driver" );   
  2. String url          =   DBConfig.getInstance().getProperty( "url" );   
  3. String username =   DBConfig.getInstance().getProperty( "username" );   
  4. String password =   DBConfig.getInstance().getProperty( "password" );  
    String driver		=	DBConfig.getInstance().getProperty("driver");
String url			=	DBConfig.getInstance().getProperty("url");
String username	=	DBConfig.getInstance().getProperty("username");
String password	=	DBConfig.getInstance().getProperty("password");
  

 

 

P.S.

请注意这三个文件的位置 , 建议放在同一个目录下

 

一. Properties 简介 
java .util. Properties 继承自 java .util.Hashtable 
Properties 类表示一个持久的属性集. Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串. 

二.基本方法 
2.1 如何从输入流中加载属性文件 
使用load(InputStream is)方法: 

Java 代码  复制代码
  1. Properties   properties  =  new   Properties ();  
  2. InputStream is =  new  FileInputStream( "conn. properties " );  
  3. properties .load(is);  
  4. is.close();  



2.2 如何读属性文件中的值 
使用getProperties(String key)方法:

Java 代码  复制代码
  1. String temp =  properties .getProperties(String key);  



<注>重载的方法getProperties(String key, String default)方法 将在查询不到值的情况下,返回default. 
即: 如果 null == properties .getProperties(String key); 
则有 default == properties .getProperties(String key, String default); 

2.3 如何获取属性文件中的所有的键 
使用propertyNames()方法,该方法返回是键的枚举.

Java 代码  复制代码
  1. Enumeration enumeration =  properties .propertyNames();  



2.4 如何修改属性文件中的值 
使用

Java 代码  复制代码
  1. setProperties(String key, String value)  

方法. 
<注>该方法调用的 Hashtable 的put方法.如果键存在,则修改值;如果键不存在,则添加值. 

2.5 如何存储属性文件到输出流 
使用 store (OutputStream os, String description)方法:

Java 代码  复制代码
  1. Properties   properties  =  new   Properties ();  
  2. OutputStream os =  new  FileOutputStream( "test. properties " );  
  3. String description =  " store   properties  to test. properties " ;  
  4. properties . store (os, description);  
  5. os.close();  



2.6 如何清空所有值 
使用

Java 代码  复制代码
  1. clear()  

 

方法. 
<注>该方法继承自 Hashtable 的clear()方法.清空哈希表. 

 

 

JAVA属性文件的操作类Propertise


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论