读取properties配置文件的工具类

xiaoxiao2021-02-28  92

public class PropertiesUtil { private static final Logger LOGGER = Logger.getLogger(PropertiesUtil.class); private final Properties props; public PropertiesUtil(final Properties props) { this.props = props; } public PropertiesUtil(final String propertiesFileName) { final Properties properties = new Properties(); InputStreamReader in = null; try { in = new InputStreamReader(new FileInputStream(this.getClass().getResource("/").getPath()+propertiesFileName), "UTF-8"); //in = PropertiesUtil.class.getClassLoader().getResourceAsStream(propertiesFileName); properties.load(in); } catch (final IOException ioe) { LOGGER.error("Unable to read " + propertiesFileName, ioe); } finally { if (in != null) { try { in.close(); } catch (final IOException ioe) { LOGGER.error("Unable to close " + propertiesFileName, ioe); } } } this.props = properties; } public String getStringProperty(final String name) { return props.getProperty(name); } public int getIntegerProperty(final String name, final int defaultValue) { String prop = props.getProperty(name); if (prop != null) { try { return Integer.parseInt(prop); } catch (final Exception ignored) { return defaultValue; } } return defaultValue; } public long getLongProperty(final String name, final long defaultValue) { String prop = props.getProperty(name); if (prop != null) { try { return Long.parseLong(prop); } catch (final Exception ignored) { return defaultValue; } } return defaultValue; } public float getFloatProperty(final String name,final float defaultValue) { String prop = props.getProperty(name); if (prop != null) { try { return Float.parseFloat(prop); } catch (final Exception ignored) { return defaultValue; } } return defaultValue; } public String getStringProperty(final String name, final String defaultValue) { final String prop = getStringProperty(name); return (prop == null) ? defaultValue : prop; } public boolean getBooleanProperty(final String name) { return getBooleanProperty(name, false); } public boolean getBooleanProperty(final String name, final boolean defaultValue) { final String prop = getStringProperty(name); return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop); } public Set<Object> keySet() { return props.keySet(); } public Collection<Object> values() { return props.values(); } /* *private final static Logger LOGGER = Logger.getLogger(PropertiesUtil.class); private final Properties prop; private final String fileName; public PropertiesUtil(String fileName) { prop = new Properties(); this.fileName = fileName; InputStreamReader is = null; try { is = new InputStreamReader(new FileInputStream(this.getClass().getResource("/").getPath()+fileName), "UTF-8"); prop.load(is); } catch (FileNotFoundException e) { LOGGER.error("", e); } catch (Exception e) { LOGGER.error("", e); } finally { if (is != null) { try { is.close(); } catch (IOException e) { LOGGER.error("", e); } } } } public String getValue(String key) { String value = prop.getProperty(key); if (value == null) { throw new IllegalArgumentException("Key: " + key + " IS NOT EXIST IN " + fileName); } return value; } */

}

在其他类中调用的方式

private static PropertiesUtil PropertiesUtil =  new PropertiesUtil("sectionDoubleBase.properties");

//参数为相应的key

public static String getSectionDoubleBase(String type){ String SectionDoubleBase=null; if(null==SectionDoubleBase){ SectionDoubleBase= PropertiesUtil.getStringProperty(type); } return SectionDoubleBase; } // 中文字符转换为UTF-8 private static String convertUTF8(String prop) { if(!StringUtils.hasText(prop)) { return null; } try { prop=new String(prop.getBytes("ISO-8859-1"),"UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return prop; }

//获取所有的值 public static List<String>  getAllSectionDoubleBase(){ //获取key的个数 Set set = PropertiesUtil.keySet(); List<String> list = new ArrayList<String>(); for(int i=0;i<set.size();i++){ list.add(getSectionDoubleBase(String.valueOf(i))); } return list; }

转载请注明原文地址: https://www.6miu.com/read-78272.html

最新回复(0)