Commit 6ae964ff8298fb5f10cf26b21c28edba058f54f7
1 parent
8f080054
Exists in
master
add properties util
Showing
1 changed file
with
51 additions
and
0 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,51 @@ |
| 1 | +package com.taover.util; | |
| 2 | + | |
| 3 | +import java.io.BufferedInputStream; | |
| 4 | +import java.io.IOException; | |
| 5 | +import java.io.InputStream; | |
| 6 | +import java.io.InputStreamReader; | |
| 7 | +import java.io.PrintWriter; | |
| 8 | +import java.io.StringWriter; | |
| 9 | +import java.util.Properties; | |
| 10 | + | |
| 11 | +import org.apache.commons.logging.Log; | |
| 12 | +import org.apache.commons.logging.LogFactory; | |
| 13 | + | |
| 14 | +public class UtilProperties { | |
| 15 | + Log log = LogFactory.getLog(this.getClass()); | |
| 16 | + | |
| 17 | + public static String getPropertiesContent(String fileName){ | |
| 18 | + InputStream inStream = UtilProperties.class.getClassLoader().getResourceAsStream("/conf/"+fileName); | |
| 19 | + try { | |
| 20 | + BufferedInputStream bis = new BufferedInputStream(inStream); | |
| 21 | + byte[] bufferByte = new byte[bis.available()]; | |
| 22 | + bis.read(bufferByte); | |
| 23 | + return new String(bufferByte, "UTF-8"); | |
| 24 | + } catch (IOException e) { | |
| 25 | + StringWriter sw = new StringWriter(); | |
| 26 | + e.printStackTrace(new PrintWriter(sw, true)); | |
| 27 | + return ""; | |
| 28 | + } | |
| 29 | + } | |
| 30 | + | |
| 31 | + public static Properties getPropertiesByFilename(String fileName){ | |
| 32 | + InputStream inStream = UtilProperties.class.getClassLoader().getResourceAsStream("/conf/"+fileName); | |
| 33 | + InputStreamReader inStreamReader = null; | |
| 34 | + try { | |
| 35 | + inStreamReader = new InputStreamReader(inStream, "utf-8"); | |
| 36 | + Properties properties = new Properties(); | |
| 37 | + properties.load(inStreamReader); | |
| 38 | + return properties; | |
| 39 | + } catch (IOException e) { | |
| 40 | + StringWriter sw = new StringWriter(); | |
| 41 | + e.printStackTrace(new PrintWriter(sw, true)); | |
| 42 | + return null; | |
| 43 | + } | |
| 44 | + } | |
| 45 | + | |
| 46 | + public static String getEnvironmentValue(String propertyFileName, String key){ | |
| 47 | + Properties prop = UtilProperties.getPropertiesByFilename(propertyFileName); | |
| 48 | + return prop.getProperty(key); | |
| 49 | + } | |
| 50 | + | |
| 51 | +} | ... | ... |