java加载properties文件的方法记录

java加载properties文件的方法记录

package com.example.tempshiro.util;

import java.io.InputStream;
import java.util.Properties;

public class LoadPropertiesUtil {

public static Properties loadProperties(String name) {
Properties prop = null;
try {
prop = new Properties();
//不同加载方式要求的文件路径不同
//1.
// InputStream is = new FileInputStream("src/main/resources/test.properties");
//2.
// InputStream is = Thread.currentThread().getContextClassLoader()
// .getResourceAsStream("test.properties");
//3.
// InputStream is = Properties.class
// .getResourceAsStream("/test.properties");
//4.
InputStream is = ClassLoader
.getSystemResourceAsStream("test.properties");
prop.load(is);
} catch (Exception e) {
e.printStackTrace();
}
return prop;
}

public static void main(String[] args) {
Properties properties = loadProperties("test.properties");
System.out.println(properties.get("name"));
}
}


 

上一篇:dubbo-invoke


下一篇:vue组件通信。prop,ref,emit的用法与区别