| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package com.mes.util;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.Properties;
- public class ConfigLoader {
- private static final String CONFIG_PATH = "config/config.properties";
- public static InputStream openConfigStream() throws IOException {
- ClassLoader cl = ConfigLoader.class.getClassLoader();
- InputStream is = cl != null ? cl.getResourceAsStream(CONFIG_PATH) : null;
- if (is == null) {
- is = ConfigLoader.class.getResourceAsStream("/" + CONFIG_PATH);
- }
- if (is == null) {
- File[] candidates = new File[]{
- new File("src/resources/" + CONFIG_PATH),
- new File(CONFIG_PATH),
- new File("bin/" + CONFIG_PATH)
- };
- for (File file : candidates) {
- if (file.isFile()) {
- return new FileInputStream(file);
- }
- }
- }
- if (is == null) {
- throw new IOException("找不到配置文件: " + CONFIG_PATH);
- }
- return is;
- }
- public static Properties loadProperties() throws IOException {
- Properties pro = new Properties();
- try (InputStream is = openConfigStream();
- BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
- pro.load(br);
- }
- return pro;
- }
- }
|