博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Property文件读取的Util类
阅读量:7227 次
发布时间:2019-06-29

本文共 7317 字,大约阅读时间需要 24 分钟。

  hot3.png

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

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

 

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 InetAddress.	 */	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 InetAddress.	 */	public InetAddress getInetAddress(final String str, final InetAddress addr) {		try {			return getInetAddress(str);		} catch (Exception ex) {			return addr;		}	}	/**	 * Get String.	 */	public String getString(final String str) throws Exception {		String value = getProperty(str);		if (value == null) {			throw new Exception(str + " not found");		}		return value;	}	/**	 * Get String.	 */	public String getString(final String str, final String s) {		try {			return getString(str);		} catch (Exception ex) {			return s;		}	}	/**	 * Get File 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 File object.	 */	public File getFile(final String str, final File fl) {		try {			return getFile(str);		} catch (Exception ex) {			return fl;		}	}	/**	 * Get Class 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 Class object */ public Class
getClass(final String str, final Class
cls) { try { return getClass(str); } catch (Exception ex) { return cls; } } /** * Get TimeZone */ 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 TimeZone */ public TimeZone getTimeZone(final String str, final TimeZone tz) { try { return getTimeZone(str); } catch (Exception ex) { return tz; } } /** * Get DateFormat 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 DateFormat object. */ public SimpleDateFormat getDateFormat(final String str, final SimpleDateFormat fmt) { try { return getDateFormat(str); } catch (Exception ex) { return fmt; } } /** * Get Date 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 Date 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 InetAddress. */ public void setInetAddress(final String key, final InetAddress val) { setProperty(key, val.getHostAddress()); } /** * Set File object. */ public void setProperty(final String key, final File val) { setProperty(key, val.getAbsolutePath()); } /** * Set DateFormat object. */ public void setProperty(final String key, final SimpleDateFormat val) { setProperty(key, val.toPattern()); } /** * Set TimeZone object. */ public void setProperty(final String key, final TimeZone val) { setProperty(key, val.getID()); } /** * Set Date object. */ public void setProperty(final String key, final Date val, final DateFormat fmt) { setProperty(key, fmt.format(val)); } /** * Set Class object. */ public void setProperty(final String key, final Class
val) { setProperty(key, val.getName()); }}
 

原文链接:

转载于:https://my.oschina.net/liux/blog/50454

你可能感兴趣的文章
崛起于Springboot2.X之配置文件详解(10)
查看>>
定时执行程序-Quartz简单实例
查看>>
【CF 应用开发大赛】MyfCMS系统
查看>>
windows下kangle虚拟主机-架设java空间的教程及心得
查看>>
Discuz! X2.5:文件目录结构
查看>>
我的友情链接
查看>>
TCP/IP协议及首部初了解
查看>>
防火墙iptables
查看>>
CUDA搭建
查看>>
memcached与PostgreSQL缓存命中机制
查看>>
百度地图路线检索(3)
查看>>
linux netstat 命令详解
查看>>
对前几篇blog的环境等的补充说明
查看>>
Curl命令使用解析大全
查看>>
MySQL日期函数
查看>>
【00】Effective Java
查看>>
.NET重构—单元测试重构
查看>>
SMB简介sabma服务(一)
查看>>
ANT简明教程
查看>>
Eclipse Luna WTP 与 Tomcat 8 的整合存在一个很头疼的 Bug
查看>>