Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to import properties files from static blocks or static methods

Shulou Source: shulou.com Published: 2022-06-02 00:04:01 10月04日 Update

Today, I will talk to you about how to import properties files from static blocks or static methods. Many people may not know much about it. In order to let you know more, Xiaobian summarizes the following contents for you. I hope you can gain something according to this article.

How to load properties files in static blocks or static methods.

Here we use ClassLoader.class.getResourceAsStream () to load the properties file in the classpath within the static block. Make sure the class is available in the classpath code: ? 12345678910112131415161718192021222324252627282930313233435 package com. java2novice.properties;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

public class MyPropInstanticBlock{private static Properties;

static{InputStream is=null;try{prop=new Properties();is=ClassLoader.class.getResourceAsStream("/sample.Properties");prop.load(is);}catch(FileNotFoundException e){e.printStackTrace();}catch(IOException e){e.printStackTrace();}

public static string getPropertyValue (string key){return prop.getProperty (key);}

Common static void main (string a[]){System.out.println ("db.host:+getPropertyValue ("db.host");System.out.println ("db.user:+getPropertyValue ("db.user");System.out.println ("db.password:+getPropertyValue ("db.password");} sample.properties? 1234db.host=appdomain.java2novice.com db.user=java2novice db.password=mypassword db.service=orcl

Input: db.host: appdomain.java2novice.com db.user: java2novelty db.password:mypassword

Having read all this, do you have any idea how to import properties files from static blocks or static methods? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.

Tags: Static file method content character string guide attribute path Chinese plus code more knowledge article industry information information channel channel enter one support Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info Redmi Docker OPPO Reno