Load content properties file

com.vingsoft.util package;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/ **
* Load Profile properties tools
*
* /
public class PropertiesUtil {

/ **
* value acquired file name and properties according to the key value
* @param fileName
* @param key
* @return
* /
public static String getPropertiesValue (String fileName, Key String) {
the Properties new new P = the Properties ();
String value = null;
the try {
the InputStream in = PropertiesUtil.class.getResourceAsStream (fileName);
p.load (in);
in.close ();

IF (p.containsKey ( key)) {
value = p.getProperty (key);
}
} the catch (Exception E) {
System.out.println ( "Get value according to the value of key properties and the file name: failed");
e.printStackTrace ();
}
value return;
}

/ **
* value according to the properties acquired file name and the key value (read Chinese)
* @param fileName
* @param key
* @return
* /
public static String getPropertiesValue2 (fileName String, String key) {
the Properties new new = P the Properties ();
String value = null;
the try {
the InputStreamReader in the InputStreamReader new new = (PropertiesUtil.class.getResourceAsStream (fileName), "GBK");
p.load (in);
in.close ();

IF (p.containsKey ( key)) {
value = p.getProperty (key);
}
} the catch (Exception E) {
System.out.println ( "Get value according to the value of key properties and the file name: failed");
e.printStackTrace ();
}
return value ;
}

public static void main (String [] args) {
path relative to the configuration file // src directory
String propertiesName = "/ Resources / datasource.properties";
// Get the value corresponding to the key according to the properties file
String ip = PropertiesUtil.getPropertiesValue (propertiesName, "isflag");
System.out.println (IP);
}
}

Guess you like

Origin www.cnblogs.com/ajing1111/p/12214506.html