greatjone

BlogJava 联系 聚合 管理
  7 Posts :: 24 Stories :: 3 Comments :: 0 Trackbacks

首先我在src目录下创建了一个test.propertise的文件:
name = jone
age 
= 20
score 
= 80

读取该文件的用例代码如下:
  1import java.io.BufferedInputStream;
  2import java.io.FileInputStream;
  3import java.io.FileNotFoundException;
  4import java.io.IOException;
  5import java.io.InputStream;
  6import java.util.Locale;
  7import java.util.Properties;
  8import java.util.PropertyResourceBundle;
  9import java.util.ResourceBundle;
 10
 11public class PropertiesReader {
 12    /**
 13     * 1.使用java.util.Properties类的load()方法
 14     */

 15    public static void method1(){
 16        try {
 17            InputStream in = new BufferedInputStream(new FileInputStream("D:/joneworkspace/joneworld/src/test.properties"));
 18            Properties p = new Properties();
 19            p.load(in);
 20            System.out.println("name is "+p.getProperty("name"));
 21            System.out.println("age is "+p.getProperty("age"));
 22            System.out.println("score is "+p.getProperty("score"));            
 23        }
 catch (FileNotFoundException e) {
 24            e.printStackTrace();
 25        }
 catch (IOException e) {
 26            e.printStackTrace();
 27        }

 28    }

 29
 30    /**
 31     * 2.使用java.util.ResourceBundle类的getBundle()方法        
 32     */

 33    public static void method2(){
 34        ResourceBundle rb = ResourceBundle.getBundle("test", Locale.getDefault());
 35        System.out.println("name is "+rb.getString("name"));
 36        System.out.println("age is "+rb.getString("age"));
 37        System.out.println("score is "+rb.getString("score"));        
 38    }

 39
 40    /**
 41     * 3.使用java.util.PropertyResourceBundle类的构造函数
 42     */

 43    public static void method3(){
 44        try {
 45            InputStream in = new BufferedInputStream(new FileInputStream("D:/joneworkspace/joneworld/src/test.properties"));
 46            ResourceBundle rb = new PropertyResourceBundle(in);
 47            System.out.println("name is "+rb.getString("name"));
 48            System.out.println("age is "+rb.getString("age"));
 49            System.out.println("score is "+rb.getString("score"));    
 50        }
 catch (FileNotFoundException e) {
 51            e.printStackTrace();
 52        }
 catch (IOException e) {
 53            e.printStackTrace();
 54        }

 55    }

 56
 57    /**
 58     * 4.使用class变量的getResourceAsStream()方法
 59     */

 60    public static void method4(){
 61        InputStream in = PropertiesReader.class.getResourceAsStream("test.properties");
 62        Properties p = new Properties();
 63        try {
 64            p.load(in);
 65            System.out.println("name is "+p.getProperty("name"));
 66            System.out.println("age is "+p.getProperty("age"));
 67            System.out.println("score is "+p.getProperty("score"));    
 68        }
 catch (IOException e) {
 69            e.printStackTrace();
 70        }
        
 71    }

 72    /**
 73     *5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
 74     */

 75    public static void method5(){
 76        InputStream in = PropertiesReader.class.getClassLoader().getResourceAsStream("test.properties");
 77        Properties p = new Properties();
 78        try {
 79            p.load(in);
 80            System.out.println("name is "+p.getProperty("name"));
 81            System.out.println("age is "+p.getProperty("age"));
 82            System.out.println("score is "+p.getProperty("score"));    
 83        }
 catch (IOException e) {
 84            e.printStackTrace();
 85        }
        
 86    }

 87
 88    /**
 89     * 6.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
 90     */

 91    public static void method6(){
 92        InputStream in = ClassLoader.getSystemResourceAsStream("test.properties");
 93        Properties p = new Properties();
 94        try {
 95            p.load(in);
 96            System.out.println("name is "+p.getProperty("name"));
 97            System.out.println("age is "+p.getProperty("age"));
 98            System.out.println("score is "+p.getProperty("score"));    
 99        }
 catch (IOException e) {
100            e.printStackTrace();
101        }
        
102    }

103    /**
104     * Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法        
105     *     示例:    InputStream in = context.getResourceAsStream(path);
106     *             Properties p = new Properties();
107     *            p.load(in);                   
108     */

109}

110
posted on 2010-06-06 21:12 jone 阅读(144) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航: