java获取配置文件.properties中的数据,具体内容如下所示:
方法太多,只写一种比较简单的。
文件test1.properties内容
1
2
3
4
5
|
test1 = 123 ; test2= 3211 properties prop = new properties(); prop.load( new fileinputstream( "src/test1.properties" )); system.out.println(prop.get( "test1" )); |
输出
123;1
简单封装一下,完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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配置文件
方法:
1
2
3
4
|
properties properties = new properties(); fileinputstream in = new fileinputstream( "**.properties" ); properties.load(in); in.close(); |
配置文件:
1
2
3
4
|
driver=com.mysql.jdbc.driver url=jdbc:mysql: //localhost:3306/test?useunicode=true&characterencoding=utf-8 username=root password= |
代码实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
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中的数据,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/nayi_224/article/details/82734332