`
bobshute
  • 浏览: 279806 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java ReadProperties

    博客分类:
  • j2se
阅读更多
package propertyFile;

import java.util.*;
import java.io.*;
 
public class ReadProperties {

		private InputStream configFile;
		private Properties props;

		public ReadProperties(){
		}

		public String getProperty(String property){
			return props.getProperty(property);
		}

		public void loadConfig() throws IOException{
			//当前类文件目录下的文件
			configFile = getClass().getResourceAsStream("purseConfig.properties");
			props = new Properties();
			props.load(configFile);
		}

		public void loadConfig(String fileName) throws IOException{
			configFile = (InputStream) new FileInputStream(new File(fileName));
			props = new Properties();
			props.load(configFile);
		}

	        public void setProp(String name,String value)
	        {
	            if (props==null)
	                props = new Properties();
	            props.put(name,value);

	        }

	 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		try {
//			ReadProperties pro = new ReadProperties();
//			//pro.loadConfig("F:/workspace/Study/src/com/properties/Config.properties");
//			pro.loadConfig();
//			System.out.println(pro.getProperty("name"));
//			System.out.println(pro.getProperty("APPKEY"));
 //		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		ReadProperties pro = new ReadProperties();
		System.out.println(pro.getPropertyByName("name"));
		System.out.println(pro.getPropertyByName("APPKEY"));
 	}

	public static String getPropertyByName(String strName){
		String str = "";
		try {
			ReadProperties pro = new ReadProperties();
			pro.loadConfig();
			str = pro.getProperty(strName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}
	
	public static String getPropertyAPPKEY(){
		String str = "";
		try {
			ReadProperties pro = new ReadProperties();
			pro.loadConfig();
			str = pro.getProperty("APPKEY");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}

 

 

 

purseConfig.properties

内容为:

 

APPKEY=n7u

 

 

 

 

分享到:
评论

相关推荐

    ReadProperties读取java项目配置文件

    参考博文: http://blog.csdn.net/joe_storm/article/details/16960735

    readProperties.java

    读取环境变量测试

    读取Properties文件的java类

    import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class ReadProperties ...

    utils-fs-read-properties:读取.properties文件的全部内容

    。特性 读取文件的全部内容。 安装 $ npm install utils-fs-read-properties 用法 var read = require ( 'utils-fs-read-properties' ) ; read(path,[options,] clbk) 读取文件的全部内容。...

    贺兰_多彩菜单 2.0.2

    19、去掉 UserControl_ReadProperties 中的判断条件,现在在窗体设计时,就可看到菜单效果!(2012-12-06) 20、解决 菜单通过 Unload 被动态删除了,绘制时的问题!(2012-12-06) 21、系统菜单的删除操作函数完成。...

Global site tag (gtag.js) - Google Analytics