码迷,mamicode.com
首页 > 编程语言 > 详细

Java读取properties文件

时间:2015-12-08 00:25:31      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

 1 package com.test;
 2 
 3 import java.util.Properties;
 4 import java.io.InputStream;
 5 import java.io.IOException;
 6 
 7 public final class ReadPropertiesUtil {
 8     private static String param1;
 9     private static String param2;
10 
11     static {
12         Properties prop = new Properties();
13 //        InputStream in1 = Object.class.getResourceAsStream("/com/property/test.properties");
14 //        InputStream in2 = ReadPropertiesUtil.class.getResourceAsStream("/com/property/test.properties");
15         InputStream in3 = ReadPropertiesUtil.class.getClassLoader().getResourceAsStream("com/property/test.properties");  
16         try {
17             prop.load(in3);
18             param1 = prop.getProperty("name").trim();
19             param2 = prop.getProperty("password").trim();
20         } catch (IOException e) {
21             e.printStackTrace();
22         }
23     }
24 
25     /**
26      * 私有构造方法,不需要创建对象
27      */
28     private ReadPropertiesUtil() {
29     }
30 
31     public static String getParam1() {
32         return param1;
33     }
34 
35     public static String getParam2() {
36         return param2;
37     }
38 
39     public static void main(String args[]) {
40         System.out.println(getParam1());
41         System.out.println(getParam2());
42     }
43 }

 

Java读取properties文件

标签:

原文地址:http://www.cnblogs.com/luomsg/p/5027874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!