How to use Java property files?

后端 未结 17 1149
时光取名叫无心
时光取名叫无心 2020-11-22 11:13

I have a list of key/value pairs of configuration values I want to store as Java property files, and later load and iterate through.

Questions:

  • Do I ne
相关标签:
17条回答
  • 2020-11-22 11:42

    Example:

    Properties pro = new Properties();
    FileInputStream in = new FileInputStream("D:/prop/prop.properties");
    pro.load(in);
    String temp1[];
    String temp2[];
    // getting values from property file
    String username = pro.getProperty("usernamev3");//key value in prop file 
    String password = pro.getProperty("passwordv3");//eg. username="zub"
    String delimiter = ",";                         //password="abc"
    temp1=username.split(delimiter);
    temp2=password.split(delimiter);
    
    0 讨论(0)
  • 2020-11-22 11:42

    1) It is good to have your property file in classpath but you can place it anywhere in project.

    Below is how you load property file from classpath and read all properties.

    Properties prop = new Properties();
    InputStream input = null;
    
    try {
    
        String filename = "path to property file";
        input = getClass().getClassLoader().getResourceAsStream(filename);
        if (input == null) {
            System.out.println("Sorry, unable to find " + filename);
            return;
        }
    
        prop.load(input);
    
        Enumeration<?> e = prop.propertyNames();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            String value = prop.getProperty(key);
            System.out.println("Key : " + key + ", Value : " + value);
        }
    
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    2) Property files have the extension as .properties

    0 讨论(0)
  • 2020-11-22 11:42

    in my opinion other ways are deprecated when we can do it very simple as below:

    @PropertySource("classpath:application.properties")
    public class SomeClass{
    
        @Autowired
        private Environment env;
    
        public void readProperty() {
            env.getProperty("language");
        }
    
    }
    

    it is so simple but i think that's the best way!! Enjoy

    0 讨论(0)
  • 2020-11-22 11:45

    There are many ways to create and read properties files:

    1. Store the file in the same package.
    2. Recommend .properties extension however you can choose your own.
    3. Use theses classes located at java.util package => Properties, ListResourceBundle, ResourceBundle classes.
    4. To read properties, use iterator or enumerator or direct methods of Properties or java.lang.System class.

    ResourceBundle class:

     ResourceBundle rb = ResourceBundle.getBundle("prop"); // prop.properties
     System.out.println(rb.getString("key"));
    

    Properties class:

    Properties ps = new Properties();
    ps.Load(new java.io.FileInputStream("my.properties"));
    
    0 讨论(0)
  • 2020-11-22 11:48

    This load the properties file:

    Properties prop = new Properties();
    InputStream stream = ...; //the stream to the file
    try {
      prop.load(stream);
    } finally {
      stream.close();
    }
    

    I use to put the .properties file in a directory where I have all the configuration files, I do not put it together with the class that accesses it, but there are no restrictions here.

    For the name... I use .properties for verbosity sake, I don't think you should name it .properties if you don't want.

    0 讨论(0)
  • 2020-11-22 11:49

    I have written on this property framework for the last year. It will provide of multiple ways to load properties, and have them strongly typed as well.

    Have a look at http://sourceforge.net/projects/jhpropertiestyp/

    JHPropertiesTyped will give the developer strongly typed properties. Easy to integrate in existing projects. Handled by a large series for property types. Gives the ability to one-line initialize properties via property IO implementations. Gives the developer the ability to create own property types and property io's. Web demo is also available, screenshots shown above. Also have a standard implementation for a web front end to manage properties, if you choose to use it.

    Complete documentation, tutorial, javadoc, faq etc is a available on the project webpage.

    0 讨论(0)
提交回复
热议问题