Properties class that reads the configuration file

Properties class that reads the configuration file

I. Introduction

If the code development profile information to be read, java comes Properties class is a profile operation is very simple class operation. Here are two ways Properties, namely how common method of profile information and Properties offered load.

Two, Properties load file in two ways

Properties first way to load configuration file

  1. Examples of the class object Properties: Properties prop = new Properties ();
  2. Object call load method, the incoming byte stream parameter type.
public class ProperTest {
    /**
     * Properties 类加载配置文件信息
     */
    public static void main(String[] args) {

        try {
            // 1.实例化 Properties类对象
            Properties prop = new Properties();

            /**
             * 2.通过对象调用load方法读取配置文件。
             *  2.1 load方法参数是字节流类型,所以需要创建一个字节流对象。
             *  2.2 将字节流对象传递给 load方法,加载文件信息。
             */

            File file = new File("src/main/resources/prop.properties");
            InputStream fileInputStream = new FileInputStream(file);
            prop.load(fileInputStream);

            //输出配置文件信息
            String propName = prop.getProperty("IAccountService");
            System.out.println(propName);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
}

 

Published 316 original articles · won praise 117 · views 420 000 +

Guess you like

Origin blog.csdn.net/m0_38039437/article/details/104933925