`
tomgooityeeee
  • 浏览: 71857 次
文章分类
社区版块
存档分类
最新评论

Property文件读取的Util类

阅读更多

    Property文件以字符串形式保存数据。

这个类可以从Property文件中读取各种转换后的常见对象,可以继续扩展。



<textarea cols="50" rows="15" name="code" class="java">package com.arui.util;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.TimeZone;
public class PropertiesUtil extends Properties {
private static final long serialVersionUID = -1810519669397594359L;
/**
* Default constructor.
*/
public PropertiesUtil() {
}
/**
* Load existing property.
*/
public PropertiesUtil(final Properties prop) {
super(prop);
}
/**
* Get boolean value.
*/
public boolean getBoolean(final String str) throws Exception {
String prop = getProperty(str);
if (prop == null) {
throw new Exception(str + " not found");
}
return prop.toLowerCase().equals("true");
}
/**
* Get boolean value.
*/
public boolean getBoolean(final String str, final boolean bol) {
try {
return getBoolean(str);
} catch (Exception ex) {
return bol;
}
}
/**
* Get integer value.
*/
public int getInteger(final String str) throws Exception {
String value = getProperty(str);
if (value == null) {
throw new Exception(str + " not found");
}
try {
return Integer.parseInt(value);
} catch (NumberFormatException ex) {
throw new Exception("PropertiesUtil.getInteger()", ex);
}
}
/**
* Get integer value.
*/
public int getInteger(final String str, final int intVal) {
try {
return getInteger(str);
} catch (Exception ex) {
return intVal;
}
}
/**
* Get long value.
*/
public long getLong(final String str) throws Exception {
String value = getProperty(str);
if (value == null) {
throw new Exception(str + " not found");
}
try {
return Long.parseLong(value);
} catch (NumberFormatException ex) {
throw new Exception("PropertiesUtil.getLong()", ex);
}
}
/**
* Get long value.
*/
public long getLong(final String str, final long val) {
try {
return getLong(str);
} catch (Exception ex) {
return val;
}
}
/**
* Get double value.
*/
public double getDouble(final String str) throws Exception {
String value = getProperty(str);
if (value == null) {
throw new Exception(str + " not found");
}
try {
return Double.parseDouble(value);
} catch (NumberFormatException ex) {
throw new Exception("PropertiesUtil.getDouble()", ex);
}
}
/**
* Get double value.
*/
public double getDouble(final String str, final double doubleVal) {
try {
return getDouble(str);
} catch (Exception ex) {
return doubleVal;
}
}
/**
* Get <code>InetAddress</code>.
*/
public InetAddress getInetAddress(final String str) throws Exception {
String value = getProperty(str);
if (value == null) {
throw new Exception(str + " not found");
}
try {
return InetAddress.getByName(value);
} catch (UnknownHostException ex) {
throw new Exception("Host " + value + " not found");
}
}
/**
* Get <code>InetAddress</code>.
*/
public InetAddress getInetAddress(final String str, final InetAddress addr) {
try {
return getInetAddress(str);
} catch (Exception ex) {
return addr;
}
}
/**
* Get <code>String</code>.
*/
public String getString(final String str) throws Exception {
String value = getProperty(str);
if (value == null) {
throw new Exception(str + " not found");
}
return value;
}
/**
* Get <code>String</code>.
*/
public String getString(final String str, final String s) {
try {
return getString(str);
} catch (Exception ex) {
return s;
}
}
/**
* Get <code>File</code> object.
*/
public File getFile(final String str) throws Exception {
String value = getProperty(str);
if (value == null) {
throw new Exception(str + " not found");
}
return new File(value);
}
/**
* Get <code>File</code> object.
*/
public File getFile(final String str, final File fl) {
try {
return getFile(str);
} catch (Exception ex) {
return fl;
}
}
/**
* Get <code>Class</code> object
*/
public Class<?> getClass(final String str) throws Exception {
String value = getProperty(str);
if (value == null) {
throw new Exception(str + " not found");
}
try {
return Class.forName(value);
} catch (ClassNotFoundException ex) {
throw new Exception("PropertiesUtil.getClass()", ex);
}
}
/**
* Get <code>Class</code> object
*/
public Class<?> getClass(final String str, final Class<?> cls) {
try {
return getClass(str);
} catch (Exception ex) {
return cls;
}
}
/**
* Get <code>TimeZone</code>
*/
public TimeZone getTimeZone(final String str) throws Exception {
String value = getProperty(str);
if (value == null) {
throw new Exception(str + " not found");
}
return TimeZone.getTimeZone(value);
}
/**
* Get <code>TimeZone</code>
*/
public TimeZone getTimeZone(final String str, final TimeZone tz) {
try {
return getTimeZone(str);
} catch (Exception ex) {
return tz;
}
}
/**
* Get <code>DateFormat</code> object.
*/
public SimpleDateFormat getDateFormat(final String str) throws Exception {
String value = getProperty(str);
if (value == null) {
throw new Exception(str + " not found");
}
try {
return new SimpleDateFormat(value);
} catch (IllegalArgumentException e) {
throw new Exception("Date format was incorrect: " + value, e);
}
}
/**
* Get <code>DateFormat</code> object.
*/
public SimpleDateFormat getDateFormat(final String str,
final SimpleDateFormat fmt) {
try {
return getDateFormat(str);
} catch (Exception ex) {
return fmt;
}
}
/**
* Get <code>Date</code> object.
*/
public Date getDate(final String str, final DateFormat fmt)
throws Exception {
String value = getProperty(str);
if (value == null) {
throw new Exception(str + " not found");
}
try {
return fmt.parse(value);
} catch (ParseException ex) {
throw new Exception("PropertiesUtil.getdate()", ex);
}
}
/**
* Get <code>Date</code> object.
*/
public Date getDate(final String str, final DateFormat fmt, final Date dt) {
try {
return getDate(str, fmt);
} catch (Exception ex) {
return dt;
}
}
/**
* Set boolean value.
*/
public void setProperty(final String key, final boolean val) {
setProperty(key, String.valueOf(val));
}
/**
* Set integer value.
*/
public void setProperty(final String key, final int val) {
setProperty(key, String.valueOf(val));
}
/**
* Set double value.
*/
public void setProperty(final String key, final double val) {
setProperty(key, String.valueOf(val));
}
/**
* Set float value.
*/
public void setProperty(final String key, final float val) {
setProperty(key, String.valueOf(val));
}
/**
* Set long value.
*/
public void setProperty(final String key, final long val) {
setProperty(key, String.valueOf(val));
}
/**
* Set <code>InetAddress</code>.
*/
public void setInetAddress(final String key, final InetAddress val) {
setProperty(key, val.getHostAddress());
}
/**
* Set <code>File</code> object.
*/
public void setProperty(final String key, final File val) {
setProperty(key, val.getAbsolutePath());
}
/**
* Set <code>DateFormat</code> object.
*/
public void setProperty(final String key, final SimpleDateFormat val) {
setProperty(key, val.toPattern());
}
/**
* Set <code>TimeZone</code> object.
*/
public void setProperty(final String key, final TimeZone val) {
setProperty(key, val.getID());
}
/**
* Set <code>Date</code> object.
*/
public void setProperty(final String key, final Date val,
final DateFormat fmt) {
setProperty(key, fmt.format(val));
}
/**
* Set <code>Class</code> object.
*/
public void setProperty(final String key, final Class<?> val) {
setProperty(key, val.getName());
}
}
</textarea>

 
0
0
分享到:
评论

相关推荐

    java的property配置文件的用法.txt

    JDK 内置的 Java.util.Properties 类为我们操作 .properties 文件提供了便利。 一. .properties 文件的形式 # 以下为服务器、数据库信息 dbPort = localhost databaseName = mydb dbUserName = root ...

    属性文件读写(java)

    import java.util.Properties; /** * @ProjectName : JavaTest * @PackageName : org.fenet.javamail * @FileName : PropertiyFileReader.java * @Describe : * @CreateTime : 2008-9-3下午04:07:01 * @...

    java-property-file-and-log4j-logging-examples:使用log4j的Java属性文件示例和日志记录示例

    用于读取.properties文件的可插拔Java Web应用程序插件 项目1:读取属性文件 建议不要在.java文件中存储服务器配置,例如数据库用户名,数据库密码,数据库IP地址,其他服务URL,FTP用户名,FTP密码和当前版本。 ...

    DWR.xml配置文件说明书(含源码)

    配置文件的allow部分定义哪些类可以建立和转换,每个被准许的类都可以有一个'create'或者'convert'配置行.下面列出的类的转换在默认情况下不需要进一步的设置. 1、所有基本类型,boolean,int,double等等 2、基本类型...

    33-工厂模式综合讲解

    import java.io.* ; import java.util.* ; public class Demo ... // 需要从文件中读取要Properties中的属性 p.loadFromXML(new FileInputStream("lxh.xml")) ; System.out.println(p) ; } };

    ssh(structs,spring,hibernate)框架中的上传下载

     其中第16行通过类路径的映射方式,将sshfile.model类包目录下的所有领域对象的映射文件装载进来,在本文的例子里,它将装载进Tfile.hbm.xml映射文件。如果有多个映射文件需要声明,使用类路径映射方式显然比直接...

    springmybatis

    2. Configuration.xml 里面 的是包含要映射的类的xml配置文件。 3. 在User.xml 文件里面 主要是定义各种SQL 语句,以及这些语句的参数,以及要返回的类型等. 开始测试 在test_src 源码目录下建立com.yihaomen.test...

    pcf8563_i2c1_r8_ruoge_ov2640通过给RTC驱动增加设备节点读取秒钟成功+直接读取I2C1获取秒钟值20160626_2201.7z

    在Android源码树中添加userspace I2C读写工具(i2c-util) 本文使用的开发板是:杭州若格科技有限公司的全志R8。CPU:CPUARM Cortex-A8 更多芯片资料请参见全志官网: http://www.allwinnertech.com/clq/r/R8.html...

    java 面试题 总结

    finalize是Object类的一个方法,在垃圾收集器执行的时候会调用被回收对象的此方法,可以覆盖此方法提供垃圾收集时的其他资源回收,例如关闭文件等。 13、sleep() 和 wait() 有什么区别? sleep是线程类(Thread)的...

    java解析给定url

    System.out.println("读取配置文件/config.properties出错"); } } /** * 程序总入口 */ private void start() { for(int i = 0; i (); i++) { URLConnection con = getConnection(configList.get(i)); ...

    SpringShiro分布式缓存版

    这个Demo体现shiro的地方主要在两个类以及shiro.xml的配置文件 CustomRealm : 处理了登录验证以及授权.. ShiroAction : 用来传递登录时的用户数据..转换为token传递给realm...之后根据结果做相应的逻辑处理.. shiro....

    超级有影响力霸气的Java面试题大全文档

    finalize是Object类的一个方法,在垃圾收集器执行的时候会调用被回收对象的此方法,可以覆盖此方法提供垃圾收集时的其他资源回收,例如关闭文件等。 16、sleep() 和 wait() 有什么区别? sleep是线程类(Thread)...

    Spring-Reference_zh_CN(Spring中文参考手册)

    13.8. Spring对分段文件上传(multipart file upload)的支持 13.8.1. 介绍 13.8.2. 使用MultipartResolver 13.8.3. 在表单中处理分段文件上传 13.9. 使用Spring的表单标签库 13.9.1. 配置标签库 13.9.2. form标签 ...

    Ext Js权威指南(.zip.001

    7.4.4 数据集:ext.util.abstractmixedcollection与ext.util.mixedcollection / 330 7.4.5 数据验证及错误处理:ext.data.validations与ext.data.errors / 332 7.4.6 模型的关系:ext.data.association、ext.data...

    +Flex+集成到+Java+EE+应用程序的最佳实践(完整源代码)

    Property 的 SOURCE 属性由 BlazeDS 读取 XML 配置文件获得: 清单 12. 配置 destination 的 id &lt;destination id="flexService"&gt; &lt;properties&gt; &lt;factory&gt;flexFactory&lt;/factory&gt; &lt;source&gt;flexService...

    将 Flex 集成到 Java EE 应用程序的最佳实践(完整源代码)

    Property 的 SOURCE 属性由 BlazeDS 读取 XML 配置文件获得: 清单 12. 配置 destination 的 id &lt;destination id="flexService"&gt; &lt;properties&gt; &lt;factory&gt;flexFactory&lt;/factory&gt; &lt;source&gt;flexService...

    精通JS脚本之ExtJS框架.part2.rar

    4.4.4 Ext.util.Observable事件 4.4.5 Ext.EventManager事件 4.4.6 Ext.EventObject事件 4.5 各种事件登记方式 4.5.1 传统式登记 4.5.2 内联式登记 4.5.3 Dom Level2登记 4.6 高级组件事件 4.7 ExtJS键盘...

    精通JS脚本之ExtJS框架.part1.rar

    4.4.4 Ext.util.Observable事件 4.4.5 Ext.EventManager事件 4.4.6 Ext.EventObject事件 4.5 各种事件登记方式 4.5.1 传统式登记 4.5.2 内联式登记 4.5.3 Dom Level2登记 4.6 高级组件事件 4.7 ExtJS键盘...

    数据连接池

    System.err.println("不能读取属性文件. " + "请确保db.properties在CLASSPATH指定的路径中"); return; } String logFile = dbProps.getProperty("logfile", "DBConnectionManager.log"); try { log = new ...

Global site tag (gtag.js) - Google Analytics