Java Load properties file Six basic ways to implement

Source: Internet
Author: User

The way Java loads the properties file is divided into two main categories: one is loaded by the load (InputStream in) method in the Import Java.util.Properties class;

The other is loaded by the Getbundle (String baseName) method of the Import Java.util.ResourceBundle class.

Note: Be sure to differentiate the path format

The implementation code is as follows:

  1 package com.util;  2 3 Import Java.io.FileInputStream;  4 Import java.io.FileNotFoundException;  5 Import java.io.IOException;  6 Import Java.io.InputStream;  7 Import java.util.Properties;  8 Import Java.util.PropertyResourceBundle; 9 Import Java.util.ResourceBundle; Ten public class Propertiesutil {The private static String BasePath = "Src/prop.properties"; String name = ""; The private static String nickname = ""; The private static String password = ""; 16 17/** 18 * I. Load the properties file using the load (InputStream in) method of the Java.util.Properties class * * * */Publ IC static String getName1 () {try {prop = new properties (); inputstre AM is = new FileInputStream (basepath); Prop.load (IS); The name = Prop.getproperty ("username");          (FileNotFoundException e) {e.printstacktrace (); catch (IOException e) {30    E.printstacktrace (); The return name;      33} 34 35/** 36 * Second, getResourceAsStream () method using class variable 37 * Note: getResourceAsStream () read path is under the same package as this class 38 * * * * * * public static String getName2 () {Prop properties = new properties (); Inpu TStream is = Propertiesutil.class. getResourceAsStream ("/com/util/prop.properties"); Prop.load (IS), prop.getproperty name = ("username"); Xception e) {e.printstacktrace (); 51} 52 53/** 54 * Three, 55 * getResourceAsStream () method of Java.lang.ClassLoader obtained using Class.getclassloader () 5 6 * The parameter of the getResourceAsStream (name) method must be a package path + file name +. suffix otherwise the null pointer exception will be reported. * * * * * */public static String GetName3                 () {Properties prop = new properties (); InputStream is = PropertiesUtil.class.getClassLoader () 62. getResourceAsStream ("com/util/prop.properties"); try {prop.load (IS); IOException (e) {e.printstacktrace () ; A. 70} 71 72/** 73 * Four, Getsystemresourceasstream () static method using Java.lang.ClassLoader class * Getsystemresourcea The parameter format of the Sstream () method is also a fixed requirement for the * * * * * * * getName4 public static String () {prop Properties = new P Roperties (); InputStream is = ClassLoader Getsystemresourceasstream ("com/util/prop.properties"); Bayi try {prop.load (is); the name = Prop.getproperty ("username"); Xception e) {e.printstacktrace (); 88} 89 90/** 91 * Five, using the Java.util.ResourceBundle class Getbundle () method 92 * Note: The parameters of this getbundle () method can only be written as package path +prop    Erties file name, otherwise the exception will be thrown * * 94 * * * * public static String GetName5 () {96     ResourceBundle RB = Resourcebundle.getbundle ("Com/util/prop"); Password = rb.getstring ("password"); 98 return password; 99}100 101/**102 * Vi. constructor function using Java.util.PropertyResourceBundle class 103 * 104 */105 public static S Tring GetName6 () {106 try {107 InputStream is = new FileInputStream (basepath); 108 Resource Bundle RB = new propertyResourceBundle (is); 109 nickname = Rb.getstring ("nickname"); catch (Fileno Tfoundexception e) {111 e.printstacktrace (); (IOException e) {113 E.printstackt Race ();}115 nickname;117}118 119/**120 * Test 121 * 122 */123 Pub Lic static void Main (string[] args) {124 System.out.println ("name1:" + propertiesutil.getname1 ()); Syst Em.out.println ("name2:" + propertiesutil.getname2 ()); 126 System.out.println ("Name3:" + propertiesutil.getname3 ()); 127 System.out.println ("Name4:" + propertiesutil.getname4 ()); System.out.println ("Password:" + Properties Util.getname5 ()); 129 System.out.println ("nickname:" + Propertiesutil.getname6 ()); 130}131}

File path:

Prop.properties file:

1 Username=mamama2 nickname=xiaoma3 password=123456

Output Result:

Java Load properties file Six basic ways to implement

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.