Java Read Properties configuration file Tool class

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.