java properties

MySysProps.java

 

      /**

* 获取所有系统属性值的java代码段

* @url http://whb.iteye.com/blog/314496 

* @param args

*/

 

package com.javaeye.lindows.util;

import java.util.Enumeration;
import java.util.Properties;

public class MySysProps {

	/**
	 * 获取所有系统属性值的java代码段
	 * @url http://whb.iteye.com/blog/314496 
	 * @param args
	 */
	public void myMethod() {
		Properties pros = System.getProperties();
		Enumeration<?> names = pros.propertyNames();
		while (names.hasMoreElements()) {
			String name = (String) names.nextElement();
			System.out.println(name + "=" + System.getProperty(name));
		}
	}

	public static void main(String[] args) {
		MySysProps testProp = new MySysProps();
		testProp.myMethod();
	}

}
 

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. }  
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>
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.

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

java.util.Properties

http://rooock.iteye.com/blog/200729

目前有个任务,要用到java.util.Properties类及其拓展知识.在这里先把预备知识梳理一下,过几天再把commons configuration整理出来. 

一.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()方法.清空哈希表. 

三.实例附件 
<注>实例中没有指明properties文件的绝对路径.那么默认是在项目根目录下的. 
当生成新文件时,使用F5刷新就能看见新文件产生了. 

Properties.rar 

end

原文地址:https://www.cnblogs.com/lindows/p/14390607.html