1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java中获取配置文件数据_Java 读取 获取配置文件.properties中的数据

java中获取配置文件数据_Java 读取 获取配置文件.properties中的数据

时间:2023-07-07 18:30:02

相关推荐

java中获取配置文件数据_Java 读取 获取配置文件.properties中的数据

java获取配置文件.properties中的数据,具体内容如下所示:

方法太多,只写一种比较简单的。

文件test1.properties内容

test1 = 123;

test2=3211

properties prop = new properties();

prop.load(new fileinputstream("src/test1.properties"));

system.out.println(prop.get("test1"));

输出

123;1

简单封装一下,完整代码

package propertis.test;

import java.io.fileinputstream;

import java.io.filenotfoundexception;

import java.io.ioexception;

import java.util.properties;

public class test {

/**

* @param args

* @throws ioexception

* @throws filenotfoundexception

*/

public static void main(string[] args) throws filenotfoundexception, ioexception {

// todo auto-generated method stub

properties prop = new properties();

prop.load(new fileinputstream("src/test1.properties"));

system.out.println(prop.get("test1"));

system.out.println(proutil.gettest1value("test1"));

system.out.println(proutil.gettest1value("test2"));

}

}

class proutil{

private static properties prop = new properties();

static{

try {

prop.load(new fileinputstream("src/test1.properties"));

} catch (filenotfoundexception e) {

// todo auto-generated catch block

e.printstacktrace();

} catch (ioexception e) {

// todo auto-generated catch block

e.printstacktrace();

}

}

public static object gettest1value(string key){

return prop.get(key);

}

}

输出

123;

123;

321

下面看下java 读取properties配置文件

方法:

properties properties = new properties();

fileinputstream in = new fileinputstream("**.properties");

properties.load(in);

in.close();

配置文件:

driver=com.mysql.jdbc.driver

url=jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8

username=root

password=

代码实现:

import java.io.fileinputstream;

import java.util.properties;

public class propertiestest {

private static final string properties_name = "db.properties";

public static string db_driver = null;

public static string db_url = null;

public static string db_user = null;

public static string db_pwd = null;

static{

fileinputstream in = null;

try{

properties properties = new properties();

in = new fileinputstream(properties_name);

properties.load(in);

db_driver = properties.getproperty("driver");

db_url = properties.getproperty("url");

db_user = properties.getproperty("username");

db_pwd = properties.getproperty("passworld");

system.out.println("读取配置信息成功!");

showconfig();

}catch(exception e){

e.printstacktrace();

system.out.println("读取配置信息失败!");

}finally{

if(in != null){

try{

in.close();

}catch(exception e){

e.printstacktrace();

}

}

}

}

private static void showconfig(){

system.out.println("-----------------------配置信息-----------------");

system.out.println("dirver: "+db_driver);

system.out.println("url: "+db_url);

system.out.println("user: "+db_user);

system.out.println("passworld: "+db_pwd);

system.out.println("----------------------------------------------");

}

public static void main(string[] args){

}

}

运行结果:

读取配置信息成功!

-----------------------配置信息-----------------

dirver: com.mysql.jdbc.driver

url: jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8

user: root

passworld: null

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

总结

以上所述是小编给大家介绍的java 读取、获取配置文件.properties中的数据,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。