Import Java.io.bufferedinputstream;import Java.io.fileinputstream;import Java.io.inputstream;import java.util.properties;/** * @ Function Description: Get resource profile information * * @zhangpj @ Created: December 3, 2015 */public final class Globals {/** *global.prop Erties file *//profile file name private static String filename = "Golbalsconfig"; Config file suffix name private static String FileType = ". Properties"; Full name of the configuration file private static String fullfilename = Filename+filetype; Configuration file location (relative path, Web-inf directory) private static String Relapath = "" +fullfilename; Configuration file contents private static Properties pps = Getallproperties (); /** * @ Function Description: Get config file * * @zhangpj @ Created: December 3, 2015 * @return */public static Properties Getallproperties () {//project root Path Strin G RootPath =globals.class.getresource ("/"). ToString (); RootPath = rootpath.substring (Rootpath.indexof ("/") +1); Configuration file full path String FilePath = Rootpath+relapath; SYSTEM.OUT.PRINTLN ("Configuration file path" +filepath); Properties Properties = new properties (); try {inputstream in = new Bufferedinputstream (new FileInputStream (FilePath)); Properties.load (in); } catch (Exception e) {System.err.println ("Get resource profile [" +fullfilename+ "] Error occurred!");} return properties; /** * Gets the value of Global.properties by key name, String type * * @param key * @return Return String * * public static string Getprop (String key) {RET Urn Pps.getproperty (key); }/** * Gets the value of Global.properties by key name, integer type * * @param key * @return Return String * * public static int getpropint (string key) {Stri ng str = globals.getprop (key); int rt = 0; try {rt = str = = null? 0:integer.parseint (Str.trim ());} catch (Exception e) {rt = 0;} return RT; /** * Gets the value of Global.properties by key name, decimal type * * @param key * @return return String */public static float Getpropfloat (string key) { String str = globals.getprop (key); float RT = 0f; try {rt = str = = null? 0f:float.parsefloat (Str.trim ());} catch (Exception e) {rt = 0f;} return RT; } public static void Main (string[] args) {System.out.println (Globals.getprop ("Ftpip")); System.out.println (Globals.getpropint ("FtpPort")); System.out.println (Globals.getprop ("FtpuserName ")); System.out.println (Globals.getprop ("Ftpuserpass")); SYSTEM.OUT.PRINTLN (Null = = Globals.getprop ("shopId222")); }}
Java Read Properties Profile tool class