📄 propertytool.java
字号:
package com.lyrisoft.util.properties;import java.util.Properties;import java.util.Vector;import java.util.StringTokenizer;import java.io.IOException;import java.io.InputStream;import java.io.FileInputStream;import com.lyrisoft.util.io.*;/** * <pre> * A helper class that: * o loads Properties files from the CLASSPATH * o reads typesafe values from Properties files * o throws Exceptions when properties are not found * </pre> * * @author Taso Lyristis <taso@lyrisoft.com> * * $Id: PropertyTool.java,v 1.3 2000/10/04 07:13:46 taso Exp $ */public class PropertyTool { /** * Will load and return a properties file, from the classpath, in the * same package as caller, and return it. * Or, throw an exception. * * This is the preferred way to load properties */ public static Properties loadProperties(Object caller, String filename) throws PropertyException { Class c = null; if (caller instanceof Class) { c = (Class)caller; } else { c = caller.getClass(); } try { InputStream is = ResourceLoader.getResource(caller, filename); return loadProperties(is); } catch (ResourceException e) { throw new PropertyException(e.toString()); } } /** * Will load and return a properties file from the specified pathname. */ public static Properties loadProperties(String relativePath) throws PropertyException { try { InputStream is = ResourceLoader.getResource(relativePath); return loadProperties(is); } catch (ResourceException e) { throw new PropertyException(e.toString()); } } public static Properties loadProperties(InputStream is) throws PropertyException { Properties p = new Properties(); try { p.load(is); } catch (Exception e) { throw new PropertyException(e.getMessage()); } finally { if (is != null) { try { is.close(); } catch (IOException e) {} } } return p; } public static String getString(String name, Properties p) throws PropertyException { String s = p.getProperty(name); if (s == null) { throw new PropertyException("No such property: " + name); } return s; } public static Vector getStringVector(String name, Properties p) throws PropertyException { String s = getString(name, p); StringTokenizer st = new StringTokenizer(s, ", "); Vector v = new Vector(); while (st.hasMoreTokens()) { v.addElement(st.nextToken()); } return v; } public static String[] getStringArray(String name, Properties p) throws PropertyException { Vector v = getStringVector(name, p); String[] strings = new String[v.size()]; v.copyInto(strings); return strings; } public static int getInteger(String name, Properties p) throws PropertyException { String s = getString(name, p); try { return Integer.parseInt(s); } catch (NumberFormatException e) { throw new PropertyException("Property " + name + " is not an integer."); } } public static float getFloat(String name, Properties p) throws PropertyException { String s = getString(name, p); try { return Float.valueOf(s).floatValue(); } catch (NumberFormatException e) { throw new PropertyException("Property " + name + " is not a float."); } } public static double getDouble(String name, Properties p) throws PropertyException { String s = getString(name, p); try { return Double.valueOf(s).doubleValue(); } catch (NumberFormatException e) { throw new PropertyException("Property " + name + " is not a double."); } } public static boolean getBoolean(String name, Properties p) throws PropertyException { String s = getString(name, p); return Boolean.valueOf(s).booleanValue(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -