下面演示java代码中获取配置文件的内容
配置文件内容(在src目录下):
代码:
import java.io.IOException; import java.util.Properties; public class getProperties { private static Properties properties = null; // 初始化 static { properties = new Properties(); try { properties.load(getProperties.class.getClassLoader().getResourceAsStream("./myConfig.properties")); }catch(IOException e) { throw new RuntimeException(e.getMessage(),e); } } // 获取值 public static String getValue(String key) { return properties.getProperty(key); } public static void main(String[] args) { String value = getProperties.getValue("username"); System.out.print("username="+value); } }
运行结果: