欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

加载属性文件Properties

程序员文章站 2022-07-04 08:45:18
...

package com.sico;

import java.io.*;
import java.util.Properties;

/**

  • ClassLoader用于读取配置文件的信息,配置文件放在classes下
    */
    @SuppressWarnings(“unused”)
    public class ConfigUtils {

    private static Properties prop = null;

    static {

     try {
         InputStream ins = ConfigUtils.class.getClassLoader().getResourceAsStream("nettyConfig.properties");
         BufferedReader br = new BufferedReader(new InputStreamReader(ins));
         prop = new Properties();
         prop.load(br);
     } catch (IOException e) {
         e.printStackTrace();
     }
    

    }

    public static Integer getClientPort() {
    Integer clientPort = Integer.valueOf((String) prop.get(“client.port”));
    return clientPort;
    }

    public static Integer getServerPort() {
    Integer serverPort = Integer.valueOf((String) prop.get(“server.port”));
    return serverPort;
    }

    public static String getClientUrl() {
    String clientUrl = (String) prop.get(“client.url”);
    return clientUrl;
    }

    public static void main(String[] args) {
    String clientUrl = getClientUrl();
    System.out.println(“clientUrl:”+clientUrl);
    }
    }