java读src内的配置文件,读src平级的配置src外面的配置文件

//访问src外平级的配置文件

Properties prop = new Properties();

InputStream in = new BufferedInputStream(new FileInputStream("conf/default.properties"));

prop.load(in); // / 加载属性列表

System.out.println(prop.getProperty("db_type"));

in.close();

-----------------------------------------------------------------------------------------

//配置 jsp访问 配置文件 访问src下的配置文件

ResourceBundle res = ResourceBundle.getBundle("config/config"); // 配置文件的路径

扫描二维码关注公众号,回复: 13352053 查看本文章

String ftpurl= res.getString("masterStationUrl"); //获取具体 的key

-----------------------------------------------------------------------------------------

默认加载配置文件 比较老的方法

static {

// 读配置文件,获取配置文件中的值,把值赋值给常量

// 先创建Properties对象

Properties pro = new Properties();

// 获取到db.properties文件的输入流

InputStream in =meishi01.class.getClassLoader()

.getResourceAsStream("db.properties"); //名称

try {

// 加载配置文件 load

pro.load(in);

} catch (IOException e) {

e.printStackTrace();

}

// 通过key来获取value值

DRIVERCLASS = pro.getProperty("driverClass");

URL = pro.getProperty("url");

USERNAME = pro.getProperty("username");

PASSWORD = pro.getProperty("password");

System.out.println(DRIVERCLASS);

}

db.properties文件(不要有 ; 号)

driverClass=abc

url=www.baidu.com

username=root

password=root

猜你喜欢

转载自blog.csdn.net/a1ccwt/article/details/83790255