读取properties配置文件的方法

package com.lzk;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Properties;

public class Main {

    public static void main(String[] args) throws IOException {
        Properties prop = new Properties();
        try {
            InputStream in = Main.class.getClassLoader().getResourceAsStream("jdbc.properties");//服务器(Tomcat)读取资源文件
//            InputStream in = new FileInputStream("src/main/resources/jdbc.properties");//本地读取
            prop.load(new InputStreamReader(in,"UTF-8"));
            Enumeration enumeration = prop.propertyNames();
            while (enumeration.hasMoreElements()){
                String key = enumeration.nextElement().toString();
                String value = prop.getProperty(key);
                System.out.println(key + ":" + value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

你可能感兴趣的:(java)