package com.cmccsi.framework.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.Iterator; import java.util.Properties; import java.util.Map.Entry; /** * Tool class for properties file operations * Get, add, modify * Note: The following method of reading the properties file will cause caching problems, and it will not work when modifying the properties file. * InputStream in = PropertiesUtils.class.getResourceAsStream("/config.properties"); * Solution: * String savePath = PropertiesUtils.class.getResource("/config.properties").getPath(); */ public class PropertiesUtils { /** * Get the data of the property file to get the value according to the key * @param fileName file name (note: the file under src is loaded, if it is under a package. Please add the package name) * @param key * @return */ public static String findPropertiesKey(String key) { try { Properties prop = getProperties(); return prop.getProperty(key); } catch (Exception e) { return ""; } } public static void main(String[] args) { Properties prop = new Properties(); InputStream in = PropertiesUtils.class .getResourceAsStream("/config.properties"); try { prop.load(in); Iterator<Entry<Object, Object>> itr = prop.entrySet().iterator(); while (itr.hasNext()) { Entry<Object, Object> e = (Entry<Object, Object>) itr.next(); System.err.println((e.getKey().toString() + "" + e.getValue() .toString())); } } catch (Exception e) { } } /** * return Properties * @param fileName file name (note: the file under src is loaded, if it is under a package. Please add the package name) * @param * @return * @throws UnsupportedEncodingException */ public static Properties getProperties() throws UnsupportedEncodingException{ Properties prop = new Properties(); String savePath = PropertiesUtils.class.getResource("/config.properties").getPath(); savePath= java.net.URLDecoder.decode(savePath,"utf-8"); //The following method reads the properties file will cache the problem // InputStream in = PropertiesUtils.class // .getResourceAsStream("/config.properties"); try { InputStream in =new BufferedInputStream(new FileInputStream(savePath)); //InputStream in = PropertiesUtils.class.getResourceAsStream("/config.properties"); prop.load(in); } catch (Exception e) { return null; } return prop; } public static Properties getjdbcProperties() throws UnsupportedEncodingException{ Properties prop = new Properties(); String savePath = PropertiesUtils.class.getResource("/jdbc.properties").getPath(); savePath= java.net.URLDecoder.decode(savePath,"utf-8"); //The following method reads the properties file will cache the problem // InputStream in = PropertiesUtils.class // .getResourceAsStream("/config.properties"); try { InputStream in =new BufferedInputStream(new FileInputStream(savePath)); //InputStream in =PropertiesUtils.class.getResourceAsStream("/jdbc.properties"); prop.load(in); in.close(); } catch (Exception e) { return null; } return prop; } /** * Write properties information * * @param key * name * @param value * value */ public static void modifyProperties(String key, String value) { try { // Read a list of attributes (key and element pairs) from the input stream Properties prop = getProperties(); prop.setProperty(key, value); String path = PropertiesUtils.class.getResource("/config.properties").getPath(); FileOutputStream outputFile = new FileOutputStream(path); prop.store(outputFile, "modify"); outputFile.close(); outputFile.flush(); } catch (Exception e) { } } }
Properties file read operation
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326435261&siteId=291194637
Recommended
Ranking